简体   繁体   中英

Can't detect jQuery click on vimeo iframe

Simple Vimeo iframe click does not work:

$('iframe').click(function(){
    alert('ok');
});

I've also tried:

$('body').click(..
$(document).on('click','iframe',..

But when user clicks video while hovering it, nothing works, it just plays the video.

if you include the query parameter api=1 in your embed code, you can access events, including those events triggered when the user clicks the video in the iframe (play,pause). You'll also probably want to include their froogaloop.js file to more easily access these events.

<iframe id="player1" src="http://player.vimeo.com/video/27855315?api=1&player_id=player1" width="400" height="225"></iframe>

它是iframe中的第三方domian,由于相同的原产地政策 ,您无法执行此操作。

try this:

$('iframe').contents()
           .find('[src*="vimeo.com"]')
           .click(
               function(){
                  alert('foo');
               }
            );

I found that before I could get anything inside the iframe to be found I needed to determine if the iframe was loaded. Then if the iframe gets loaded after page load or reloaded later in the process, your click function works.

jQuery('#iframe').load(function() {
   jQuery('#iframe').contents().find('#play-button').click(function () {
        // do your stuff
    });
}

** this may or may not work cross-domain, but determining if the iframe loaded can be used as a hackish method of determining if something has happened in the iframe. If you created a "play" button on your domain on top of the iframe it could be used to load the iframe via a click function after page load and then your load function could contain your slideshow pause.

Unfortunately, you cannot track clicks in cross-domain iframes due to same origin policy, as epascarello has previously said.

You could set up a "thumbnail" that the user clicks, which would pull up that popup and then subsequently replace the thumbnail with the actual video. Just embed the video you want, but keep it as a hidden div, then use .show() when you want it to start playing.

Source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM