【问题标题】:Javascript Parser errorJavascript 解析器错误
【发布时间】:2017-04-27 19:05:25
【问题描述】:

我有一个带有海报和 CSS 播放叠加按钮的 HTML5 视频。我正在尝试在视频结束后加载视频,以便再次显示海报和播放叠加按钮。

我尝试了以下代码,但在最后一行出现 Parser 错误,谁能帮助我,让我知道我做错了什么!?

$(document).ready(function() {
  $('.video').parent().click(function () {
    if ($(this).children(".video").get(0).paused) {
      $(this).children(".video").get(0).play();
      $(this).children(".playpause").fadeOut();
    } else {
      $(this).children(".video").get(0).pause();
      $(this).children(".playpause").fadeIn();
    }
  });

  var video= $(".video").get(0);   
  video.addEventListener('ended',function () {
    video.load(); 
    $(".playpause").show();                              
  }, false);
});

【问题讨论】:

  • 准确的错误信息是...?
  • 看不到错误。如果您正确缩进代码会有所帮助。
  • 如果你也添加 HTML 会有帮助。
  • 代码不会对我造成任何错误,可能是因为没有执行适当的条件或回调。在您的浏览器中尝试向下钻取/单击错误。使用 Firebug 或 Chrome 开发工具查看错误消息的确切来源。
  • @j08691 这是错误 undefined:1 Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause(). 。要重现,请等到视频播放完毕,然后立即快速连续点击两次"click"jsfiddle.net/q1wnLwa3/1

标签: javascript html5-video


【解决方案1】:

防止

Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause().

错误,将 if 语句包装在 click 处理程序内 setTimeout() 调用内。

另见How to prevent "The play() request was interrupted by a call to pause()" error?

$(document).ready(function() {
  $('.video').parent().click(function () {
    var vid = $(this).children(".video").get(0);
    setTimeout(function() {
    if (vid.paused) {
      vid.play();
      $(this).children(".playpause").fadeOut();
    } else {
      vid.pause();
      $(this).children(".playpause").fadeIn();
    }
    })
  });

  var video= $(".video").get(0);   
  video.addEventListener('ended',function () {
    video.load(); 
    $(".playpause").show();                              
  }, false);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
click<br>
<span class="playpause">play pause</span>
<video controls class="video" poster="https://placehold.it/350x150"  src="https://nickdesaulniers.github.io/netfix/demo/frag_bunny.mp4"></video>
</div>

【讨论】:

    猜你喜欢
    • 2016-01-31
    • 2010-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    相关资源
    最近更新 更多