当我调用此函数" showMe(calcTotal(myNumberArray));"它在控制台中有效,但在代码中调用时它不起作用。对不起,如果我的代码惹恼了你的眼睛,或者从那时起就没有。它应该计算总数。我想知道为什么浏览器没有看到调用或为什么值没有显示在屏幕上。
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
</style>
</head>
<body>
<button id="hit">hit</button>
<div id="number"></div>
<div id="arrayOutput"></div>
<div id="someId"></div>
<div id="out2"></div>
<script>
//Services helper functon
var myNumberArray = [];
document.getElementById('hit').onclick = function randomNumber() {
var card = Math.floor(Math.random() * 10) + 1;
document.getElementById('number').innerHTML=card;
myNumberArray.push(card);
var number =myNumberArray.value;
var arrayOutput = document.getElementById('number');
var someId = document.getElementById('someId');
someId.innerHTML = myNumberArray;
};
//var output = myNumberArray = calcTotal(list);
function calcTotal(myNumberArray) {
var total = 0;
for(var i = 0; i < myNumberArray.length; i++){
total += myNumberArray[i];
}
return total;
}
//document.getElementById('out2').innerHTML = out2;
what = calcTotal(myNumberArray);
var what= calcTotal(myNumberArray);
function showMe(VAL) {
var parent = document.getElementById('out2');
parent.innerHTML = VAL;
}
showMe(calcTotal(myNumberArray));
</script>
</body>
</html>
&#13;
答案 0 :(得分:0)
如果您想要点击按钮更新总值,则需要在点击处理程序中调用showMe(calcTotal(myNumberArray));
。
//Services helper functon
var myNumberArray = [];
document.getElementById('hit').onclick = function randomNumber() {
var card = Math.floor(Math.random() * 10) + 1;
document.getElementById('number').innerHTML = card;
myNumberArray.push(card);
var number = myNumberArray.value;
var arrayOutput = document.getElementById('number');
var someId = document.getElementById('someId');
someId.innerHTML = myNumberArray;
showMe(calcTotal(myNumberArray));
};
//var output = myNumberArray = calcTotal(list);
function calcTotal(myNumberArray) {
var total = 0;
for (var i = 0; i < myNumberArray.length; i++) {
total += myNumberArray[i];
}
return total;
}
//document.getElementById('out2').innerHTML = out2;
what = calcTotal(myNumberArray);
var what = calcTotal(myNumberArray);
function showMe(VAL) {
var parent = document.getElementById('out2');
parent.innerHTML = VAL;
}
showMe(calcTotal(myNumberArray));
&#13;
<button id="hit">hit</button>
<div id="number"></div>
<div id="arrayOutput"></div>
<div id="someId"></div>
<div id="out2"></div>
&#13;