Jquery-Youtube链接到嵌入式



试图将youtube链接转换为嵌入式,但我的jquery似乎没有从链接中获取视频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>

我一无所知。。。

您可以使用split。。

$('.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/

最新更新