js怎么调用函数中的值

能帮忙指点下这代码应该怎么改,怎么没有结果啊,我就是想在函数中赋值,然后输出调用

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript 函数</h2>

<p>本例调用函数把华氏度转换为摄氏度:</p>

<p id="gwp1"></p>

<script>
function () {
    var gwp=60;
    var gwp1=gwp+10;
    document.getElementById("gwp1").innerHTML =gwp1,
},
</script>

</body>
</html>

5个回答

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript 函数</h2>

<p>本例调用函数把华氏度转换为摄氏度:</p>

<p id="gwp1"></p>

<script>
cal();

function cal() {
    var gwp=60;
    var gwp1=gwp+10;
    document.getElementById("gwp1").innerHTML =gwp1,
},
</script>

</body>
</html>
guangcaiwudong
Kevin.Y.K 先来后到,有用请采纳
6 个月之前 回复
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript 函数</h2>

<p>本例调用函数把华氏度转换为摄氏度:</p>

<p id="gwp1"></p>

<script>
function a() {
    var gwp=60;
    var gwp1=gwp+10;
    document.getElementById("gwp1").innerHTML =gwp1,
},
a();
</script>

</body>
</html>

使用自执行函数,你写的是匿名函数,需要自执行才起作用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h2>JavaScript 函数</h2>

<p>本例调用函数把华氏度转换为摄氏度:</p>

<p id="gwp1"></p>
<script>
    (function (){
        var gwp=60;
        var gwp1=gwp+10;
        document.getElementById("gwp1").innerHTML =gwp1;
    })()
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript 函数</h2>

<p>本例调用函数把华氏度转换为摄氏度:</p>

<p id="gwp1"></p>

<script>
window.onload = function(){
  var gwp=60;
    var gwp1=gwp+10;
    alert(gwp1);
    document.getElementById("gwp1").innerHTML =gwp1;
  }
</script>

</body>
</html>

你的写法,页面 加载的时候根本不会调用你的方法,肯定不会赋值

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问