如何使用javascript在h2标记中显示最终结果

如何使用javascript在h2标记中显示最终结果,javascript,Javascript,我需要一些JavaScript数学方面的帮助。我想在一个h2标记中显示最终结果,但我不知道怎么做(我不熟悉JavaScript)。请提前帮助和感谢 <!doctype html> <html> <head> <title>Do Math</title> </head> <body> <input type = "number" id = "my_input1"/> <input

我需要一些JavaScript数学方面的帮助。我想在一个h2标记中显示最终结果,但我不知道怎么做(我不熟悉JavaScript)。请提前帮助和感谢

<!doctype html>
<html>
<head>
<title>Do Math</title>
</head>
<body>
    <input type = "number" id = "my_input1"/>
    <input type = "number" id = "my_input2"/>
    <input type = "button" value = "Add them Together" onclick = "doMath();"/>
    <script type = "text/javascript">
        function doMath() {
            var my_input1 = document.getElementById('my_input1').value;
            var my_input2 = document.getElementById('my_input2').value;
            //Add them Together and Display

            var sum = parseFloat(my_input1) + parseFloat(my_input2);
            document.write("<h2>The sum is </h2>", sum);
        }
    </script>
</body>
</html>

做数学
函数doMath(){
var my_input1=document.getElementById('my_input1').value;
var my_input2=document.getElementById('my_input2').value;
//将它们添加到一起并显示
var sum=parseFloat(my_input1)+parseFloat(my_input2);
文件。填写(“总和为”,总和);
}
试试这个

<html>
    <head>
      <title>Do Math</title> 
    </head>
    <body>
      <input type = "number" id = "my_input1"/>
      <input type = "number" id = "my_input2"/>
      <input type = "button" value = "Add them Together" onclick = "doMath();"/>
      <h2 id="output"></h2>
      <script type = "text/javascript">
              function doMath() {
                  var my_input1 = document.getElementById('my_input1').value;
                  var my_input2 = document.getElementById('my_input2').value;
                  //Add them Together and Display

                  var sum = parseFloat(my_input1) + parseFloat(my_input2);
                  document.getElementById('output').innerHTML = "The sum is " +  sum;
               }
       </script>
    </body>
   </html>

做数学
函数doMath(){
var my_input1=document.getElementById('my_input1').value;
var my_input2=document.getElementById('my_input2').value;
//将它们添加到一起并显示
var sum=parseFloat(my_input1)+parseFloat(my_input2);
document.getElementById('output').innerHTML=“总和为”+sum;
}

请参见

只需获取一个h2标记,并使用

html tag:
<h2 id='ele'></h2>


javascript:
document.getElementById('ele').innerHTML="the sum is "+sum;
html标记:
javascript:
document.getElementById('ele').innerHTML=“总和为”+sum;

不要使用
文档。加载页面后写入
。这是您想要的
getElementByTagName
应该是
元素
,它返回一个数组(
NodeList
),您也可以使用
getelementById('one'))
为指定的h2元素指定一个id,如
,如果您喜欢,请单击正确的信号使其正确