gpt4 book ai didi

javascript - 如何正确调用我的 Javascript 函数?

转载 作者:行者123 更新时间:2023-12-01 15:55:24 25 4
gpt4 key购买 nike

我刚开始学习 JS,我找不到我的代码有什么问题:

<!DOCTYPE html>
<html>
<body>
<script>
myfunc(){
var no1=parseInt(document.getElementById("n1").value);
var no2=parseInt(document.getElementById("n2").value);
document.getElementById("f").innerHTML=no1+no2;
}
</script>
num1: <input type="text" id="n1"/>+num2: <input type="text" id="n2"/>
<button onclick="myfunc()">=</button>
<p id="f"></p>
</body>
</html>

最佳答案

只需使用function 保留字声明您的函数

<!DOCTYPE html>
<html>
<body>
<script>
function myfunc(){
var no1=parseInt(document.getElementById("n1").value);
var no2=parseInt(document.getElementById("n2").value);
document.getElementById("f").innerHTML=no1+no2;
}
</script>
num1: <input type="text" id="n1"/>+num2: <input type="text" id="n2"/>
<button onclick="myfunc()">=</button>
<p id="f"></p>
</body>
</html>

关于javascript - 如何正确调用我的 Javascript 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63240052/

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