jQuery/HTML5 modal video if autoplay is present play video [duplicate]

痴心易碎 提交于 2019-12-12 05:01:32

问题


To make it easier to understand please take a look at the Codepen: http://codepen.io/hennysmafter/pen/YqmLKR

The codepen is a simpler version of the actual code with only the parts that are necessary!

In the codepen you have two popups one popup has a video element with autoplay and the other has a video element without autoplay

On document ready all video elements on the page are paused this cannot be changed because of other videos in the actual code

Now I just need to have the code adapted so that:

  1. On click popup opens and class is added opened (already there)
  2. If video element inside popup has autoplay then play the video (not working)
  3. Or no autoplay so do nothing

    if ($('.modal-box.opened').find('video').attr(autoplay) == true) {
        $('.modal-box.opened').find('video').each(function() {
          $(this).get(0).play();
    });
    }
    

回答1:


You've two somewhat common mistakes: You only used 1 equals sign in the part where you check if autoplay is true.

A single = is used for setting variables where as two is used for checking equality.

Edit: You are also attempting to use traditional HTML style attribute checking on a jQuery object. To check an attribute in jQuery, you need to use .attr(attributeName)

Therefore, the correct line would be if ($('.modal-box.opened').find('video').attr(autoplay) == true) {



来源:https://stackoverflow.com/questions/37398260/jquery-html5-modal-video-if-autoplay-is-present-play-video

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!