防止点击暂停视频

时间:2017-04-30 15:41:12

标签: javascript jquery html5

如何防止暂停视频点击? e.preventDefault;无效。

<video width="320" height="240" controls>
  <source src="video.mp4" type="video/mp4">
  Your browser does not support the video tag.
</video>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script>
  $(function () {
    $('video').click(function (e) {
      e.preventDefault;
    });
  });
</script>

2 个答案:

答案 0 :(得分:2)

这是解决您问题的方法。

我们添加事件监听器onpause。每当我们检测到pause事件被触发时,我们都会运行play()方法。

我们还可以做以下事情:(与下面的例子相同,但简化了)

<video id="bunny" width="400" controls  onPause="this.play();">

参见工作示例。

&#13;
&#13;
var vid = document.getElementById('bunny');
vid.addEventListener('pause', function(e) {
  vid.play();
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<video id="bunny" width="400" controls  >
  <source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
  Your browser does not support HTML5 video.
</video>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

删除controls并添加autoplay

<video width="320" height="240" autoplay>
  <source src="http://www.uio.no/tjenester/it/multimedia/utvikling/video/test.mp4" type="video/mp4">
  Your browser does not support the video tag.
</video>

相关问题