我的代码将数字递增或递减 1。问题是“testNum”,它是一个全局变量,应该反映递增或递减,但是当我在控制台中检查“testNum”的值时,它总是 20。我通过了“testNum” " 到我的函数并返回它。为什么它不反映增量?
小提琴:http: //jsfiddle.net/FnUXw/1/
<input type="button" id="btnDown" value="-">
<span id="amountDiv">amount div</span>
<input type="button" id="btnUp" value="+">
<script>
var testNum = 20;
amountDiv.innerHTML = testNum;
function makeButtonIncrement(button, upDown, displayDiv, variable) {
function changeValue () {
if (upDown == "up") {
variable++;
}
if (upDown == "down") {
variable--;
}
displayDiv.innerHTML = variable;
}
button.onclick = changeValue;
return variable;
}
makeButtonIncrement(document.getElementById('btnDown'), "down", document.getElementById('amountDiv'), testNum);
makeButtonIncrement(document.getElementById('btnUp'), "up", document.getElementById('amountDiv'), testNum);
</script>