请检查: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>
我一无所知......
答案 0 :(得分:2)
我认为问题是我在DABphlXEyW8e
到DABphlXEyW8
答案 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尝试