0

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>

私は無知です...

4

1 に答える 1

1

あなたはただ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/

于 2013-02-15T22:58:52.013 に答える