3 回答
TA贡献1871条经验 获得超8个赞
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<p id="num1">5</p>
<p id="num2">5</p>
<p id="res">1</p>
<script>
window.onload = function () {
var num1 = document.getElementById("num1").innerHTML;
var num2 = document.getElementById("num2").innerHTML;
var res = parseInt(num1) + parseInt(num2);
document.getElementById("res").innerHTML = res;
};
</script>
</body>
</html>
TA贡献1836条经验 获得超13个赞
您应该使用 Number(document.getElementById("num1").innerText),因为 p 标签的 value 属性不存在。
TA贡献1852条经验 获得超1个赞
您已使用“值”而不是innerHTML 来获取数字。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<p id="num1">5</p>
<p id="num2">5</p>
<p id="res">1</p>
</body>
<script>
var num1 = parseInt(document.getElementById("num1").innerHTML);
var num2 = parseInt(document.getElementById("num2").innerHTML);
var res = num1 + num2;
document.getElementById("res").innerHTML = res;
</script>
</html>
添加回答
举报