不起作用,在 html 中显示 javascript 变量

简单的问题,变量total没有显示在html页面中,你能告诉我为什么这不起作用吗?我对编码很陌生。


 var addtime = [0,2];


 function totaltime(){

  var total = 0;

   for(var i in addtime) {

     total += addtime[i];

   }

   document.getElementById("totalTime").innerHTML = total;

 }

html


<h1>Total Time: <span id="totalTime"></span></h1>


梦里花落0921
浏览 102回答 2
2回答

有只小跳蛙

您缺少调用该功能totaltime();var addtime = [0,2];function totaltime(){&nbsp; &nbsp;var total = 0;&nbsp; &nbsp;for(var i in addtime) {&nbsp; &nbsp; &nbsp; total += addtime[i];&nbsp; &nbsp;}&nbsp; &nbsp;document.getElementById("totalTime").innerHTML = total;&nbsp;}&nbsp;&nbsp;totaltime();&nbsp; &nbsp;<h1>Total Time: <span id="totalTime"></span></h1>

一只名叫tom的猫

你没有调用该函数var addtime = [0,2];&nbsp;function totaltime(){&nbsp; var total = 0;&nbsp; &nbsp;for(var i in addtime) {&nbsp; &nbsp; &nbsp; total += addtime[i];&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;document.getElementById("totalTime").innerHTML = total;&nbsp;}totaltime()<h1>Total Time: <span id="totalTime"></span></h1>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5