【问题标题】:Jquery - Youtube link to embedded [duplicate]Jquery - 嵌入的 Youtube 链接 [重复]
【发布时间】:2013-02-15 22:40:04
【问题描述】:

试图将 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>

我一无所知...

【问题讨论】:

标签: javascript jquery html


【解决方案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/

【讨论】:

  • 这很奇怪。一旦我摆弄它,它就会停止工作......另一方面,如果我硬编码 id,它会工作得很好,但它不会从链接中获取 id......我很困惑
  • 查看链接:jsfiddle.net/T6Nja
猜你喜欢
  • 1970-01-01
  • 2012-04-11
  • 1970-01-01
  • 2013-02-01
  • 1970-01-01
  • 2017-04-28
  • 2011-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多