JQuery youtube链接到嵌入式无法正常工作

时间:2013-02-15 23:32:58

标签: javascript jquery html

请检查:http://jsfiddle.net/T6Nja/

我的JQuery:

$('.training').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>';
});

}); 我的HTML:

<div class="training">http://www.youtube.com/watch?v=DABphlXEyW8e</div><div class="training">http://youtu.be/DABphlXEyW8f</div>

我一无所知......

2 个答案:

答案 0 :(得分:2)

我认为问题是我在DABphlXEyW8eDABphlXEyW8

答案 1 :(得分:0)

第一个确实有效(在某种意义上它从div中的url获取id),因为它包含:

watch?v=

在此行中提取id时需要:

var id = v.split('watch?v=')[1]; // get the id so you can add to iframe

但它在jsfiddle中抛出了这个错误:

Unsafe JavaScript attempt to access frame with URL
http://jsfiddle.net/T6Nja/ from frame with URL 
http://www.youtube.com/embed/DABphlXEyW8e.
Domains, protocols and ports must match.

第二个“培训”div不起作用,因为它不包含:

watch?v=

需要提取Id

您需要将html更新为:

<div class="training">http://www.youtube.com/watch?v=DABphlXEyW8e</div>

<div class="training">http://www.youtube.com/watch?v=DABphlXEyW8f</div>

Youtube embed: Unsafe JavaScript attempt to access frame

上面的链接可以帮助您解决不安全的JavaScript尝试