javascript 如何动态添加 wmode=transparent 到 Youtube 嵌入代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8818036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to dynamically add wmode=transparent to Youtube embed code?
提问by Rob
I have several Youtube videos that are added through a CMS by a client. I need to add the following to all Youtube src links:
我有几个由客户通过 CMS 添加的 Youtube 视频。我需要将以下内容添加到所有 Youtube src 链接:
?wmode=transparent
How would I do that?
我该怎么做?
An example of the Youtube embed code is as follows:
Youtube 嵌入代码示例如下:
<iframe width="515" height="292" src="http://www.youtube.com/embed/p8IB-5PbL9U" frameborder="0" allowfullscreen></iframe>
The reason for doing this is because I have a javascript menu that is going behind a Youtube video and I've read that this is how you fix it.
这样做的原因是因为我有一个位于 Youtube 视频后面的 javascript 菜单,我读到这是您修复它的方法。
The client isn't technical at all and just having them get the embed code from Youtube is a struggle, so it needs to be added dynamically.
客户端根本不是技术人员,只是让他们从 Youtube 获取嵌入代码是一件很困难的事情,因此需要动态添加。
回答by Shadow Wizard is Ear For You
If you just need to add ?wmode=transparent
to all the frames, have this JS code:
如果您只需要添加?wmode=transparent
到所有框架,请使用以下 JS 代码:
window.onload = function() {
var frames = document.getElementsByTagName("iframe");
for (var i = 0; i < frames.length; i++) {
frames[i].src += "?wmode=transparent";
}
}
回答by jordancpaul
Are you fixed on using the iframe? It makes this a lot more difficult as you are unable to access the underlying object directly. If you can, it would be better to directly embed the object on your page as such
你确定使用 iframe 吗?这使得这变得更加困难,因为您无法直接访问底层对象。如果可以,最好直接将对象嵌入到您的页面中
<object width="515" height="292">
<param name="movie" value="http://www.youtube.com/v/p8IB-5PbL9U"></param>
<param name="allowFullScreen" value="true"></param>
<param name="wmode" value="transparent"></param>
<embed src="http://www.youtube.com/v/p8IB-5PbL9U"
type="application/x-shockwave-flash"
allowfullscreen="true"
wmode="transparent"
width="515" height="292">
</embed>
</object>
回答by MarkSmits
Depending on your CMS you could do this through plain PHP, by just adding it at the end of each url, or your or make jQuery do the work for you.
根据您的 CMS,您可以通过普通的 PHP 完成此操作,只需将其添加到每个 url 的末尾,或者您的或让 jQuery 为您完成工作。
$('iframe').each(function() {
$(this).attr("src", $(this).attr("src") + '?wmode=transparent')
});