所以我有一项工作要上学,我需要做一个游戏,其中要显示音频播放了多少次。我真的不知道该怎么做,我尝试了互联网上的这段代码,并尝试了一个视频,但它说


  “无法将属性'innerHTML'设置为null”




var iterations = 1;

document.getElementById('iteration').innerText = iterations;

myVideo.addEventListener('ended', function() {

  if (iterations < 5) {
    this.currentTime = 0;
    this.play();
    iterations++;
    document.getElementById('iteration').innerText = iterations;
  }
}, false);

<video width="320" height="240" id="myVideo" controls>
    <source src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" type="video/mp4" />
    <source src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.ogv" type="video/ogg" />
Your browser does not support the video tag.
</video>

最佳答案

没有元素可以使用变量迭代进行更新

确保在DOM中创建“ iteration”元素后触发JavaScript,否则您将收到错误消息


  未捕获的TypeError:无法将属性'innerText'设置为null
      在 ...




 // set iteration to 1
 var iterations = 1;

 document.getElementById('iteration').innerText = iterations;

      myVideo.addEventListener('ended', function () {

      if (iterations < 5) {

           this.currentTime = 0;
           this.play();
           iterations ++;

           document.getElementById('iteration').innerText = iterations;

       }

}, false);

<video width="320" height="240" id="myVideo" controls>
        <source src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" type="video/mp4" />
        <source src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.ogv" type="video/ogg" />
        Your browser does not support the video tag.
    </video>
<p>Iteration: <span id="iteration"></span></p>

关于javascript - 计算回放音频的次数-Javascript或Jquery,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47563126/

10-12 14:07
查看更多