单击时,为什么按钮没有向演示中显示的x添加5?
<h1>The += Operator</h1>
<p id="demo"></p>
<script>
var x = 10;
document.getElementById("demo").innerHTML = x;
function add(){
x = x+=5;
}
</script>
<button onclick="add()"> += </button>
</body>
答案 0 :(得分:-1)
解决了它,显然函数中的代码不能影响它之外的变量,这是我的解决方案
<!DOCTYPE html>
<html>
<body>
<h1>The += Operator</h1>
<p id="demo"></p>
<script>
var x = 10;
function add() {
x = x+=5;
document.getElementById("demo").innerHTML = x;
}
</script>
<button onclick="add()"> += </button>
</body>
</html>