开发者

Use JavaScript and jQuery to Make a Hyperlink to Play Embedded YouTube Video

Does anyon开发者_如何学Ce know how I would use JavaScript with jQuery to make a hyperlink to play an embedded YouTube video? I know that I could do:

var player = document.getElementById("player");
$("#link").click(function(event){
event.preventDefault();
player.playVideo();
});

However, wouldn't this only work if I am using <embed> or <object>? I am using <iframe> to embed the video on my page. Would the document.getElementById("player") still work on the <iframe id="player">?


$('#player') will still work and find the iFrame. However, to find elements within that iFrame, you will have to use $('selector', $('#player').contentDocument). To find elements in your document while executing javascript within the iframe, you will have to use parent.$('selector'). If you want to play a youtube video you can simply use this:

$('.youtubeLink').bind('click', function(e) {
    e.preventDefault();
    $('iframe').attr('src',$(this).attr('href'));
});

DEMO Btw, use the embedded link you can find on the youtube page as href of your a

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜