How do I add an event listener?

岁酱吖の 提交于 2019-12-12 00:52:23

问题


Im using swfobject to embed the vimeo video, it's pretty simple,

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.


回答1:


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

try using this...

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




回答2:


If using Flash Embeds, where play() is prefixed to api_play(), the need to prefix also includes addEventListener():

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

This is demonstrated under Flash Embed Code:

To add a listener for the finish event:

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



回答3:


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);



回答4:


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. English is not my first language. Sorry if it's difficult to understand.



来源:https://stackoverflow.com/questions/18841618/how-do-i-add-an-event-listener

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