2013-02-15 62 views
0

试图把YouTube的链接到嵌入式,但我的jQuery似乎没有从链接jQuery的 - Youtube链接到嵌入式

抓取视频ID,我的JQuery:

$('.a').html(function(i, html) { 
return html.replace(/(?:http:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?(.+)/g,'<iframe width="150" height="150" src="http://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>'); 
}); 

我的HTML:

<div class="a">http://www.youtube.com/watch?v=w_yudtBvhCsc</div> 
<div class="a">http://www.youtube.com/watch?v=w_yudtBvhCsd</div> 

林无言以对......

+0

我猜你在HTML无法控制? – 2013-02-15 22:41:36

+0

使用'embed'或'object' http://www.w3schools.com/html/html_object.asp – Dom 2013-02-15 22:44:45

+1

@Dom只要你知道,w3schools在这里皱起了眉头。改用MDN:https://developer.mozilla.org/ – 2013-02-15 22:46:26

回答

1

你可以使用分裂..

$('.a').html(function(i,v){ 
    var id = v.split('watch?v=')[1]; // get the id so you can add to iframe 
    return '<iframe width="150" height="150" src="http://www.youtube.com/embed/' + id + '" frameborder="0" allowfullscreen></iframe>'; 
}); 

http://jsfiddle.net/nzvYv/

+0

这很奇怪。只要我捣鼓它,它停止工作...另一个说明,如果我的硬编码身份证,它会工作得很好,但它没有抓住链接的ID ... IM非常困惑 – 2013-02-15 23:17:34

+0

请参阅链接:http:/ /jsfiddle.net/T6Nja/ – 2013-02-15 23:22:57

相关问题