简体   繁体   English

如何添加事件监听器?

[英]How do I add an event listener?

Im using swfobject to embed the vimeo video, it's pretty simple, 我使用swfobject嵌入vimeo视频,这非常简单,

var vimPlayer;
function vimeo_player_loaded(id){        
    vimPlayer = document.getElementById(id);           
    vimPlayer.addEventListener('play', function(){
        alert('Playing');
    });
    vimPlayer.api_play();  
}

vimPlayer.api_play(); Does play the video just fine! 播放视频是否正常! However, How can I bind a listener to that function so that I can perform other tasks? 但是,如何将侦听器绑定到该函数,以便执行其他任务? The listener I have added does not fire at all. 我添加的监听器根本不会触发。

$('video').bind('play', function (e) {
    // do something
});

try using this...

http://www.w3.org/2010/05/video/mediaevents.html http://www.w3.org/2010/05/video/mediaevents.html

Adding event listener takes three params, here's an example: 添加事件监听器需要三个参数,这是一个示例:

function doSomething() {
   alert('Image clicked');
}

var myButton = document.getElementById('my_button_id');

myButton.addEventListener('click', doSomething, false);

If using Flash Embeds, where play() is prefixed to api_play() , the need to prefix also includes addEventListener() : 如果使用Flash Embeds,其中play()api_play()为前缀,则需要添加前缀还包括addEventListener()

vimPlayer.api_addEventListener('play', function () {
    alert('Playing');
});

This is demonstrated under Flash Embed Code : Flash嵌入代码对此进行了演示:

To add a listener for the finish event: finish事件添加侦听器:

 document.getElementById('vimeo_player').api_addEventListener('finish', function(event) { // do stuff here }); 

I was stacking at exactly the same problem. 我当时正面临着完全相同的问题。 As long as I tried countless times, 只要我尝试了无数次

function vimeo_player_loaded(id){        
    vimPlayer = document.getElementById(id);           
    vimPlayer.api_addEventListener('play', 'vimeo_play');
    vimPlayer.api_play();  
}
function vimeo_play(){
    console.log("play");
}

This should work fine if the player_id and id in HTML are same. 如果HTML中的player_id和id相同,则应该可以正常工作。 English is not my first language. 英语不是我的母语。 Sorry if it's difficult to understand. 对不起,如果很难理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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