gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 18:19:38 25 4
gpt4 key购买 nike

我需要一些关于 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>

最佳答案

试试这个

<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>

参见DEMO

关于javascript - 如何使用javascript在h2标签中显示最终结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18941711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com