尝试编写Javascript循环通过YouTube iFrame嵌入
Trying to Write Javascript to Loop Through YouTube iFrame Embeds
我正在尝试和失败写一个drop-in YouTube嵌入播放器为我的用户,这样他们只需要改变一行改变任何和所有的YouTube播放器在一个页面上。我把这个放到我的页面,但是什么也没发生。
<script>
// Replace line below with video id(s), e.g. ["dQw4w9WgXcQ"] or ["dQw4w9WgXcQ","b1WWpKEPdT4"]
var ids = ["dQw4w9WgXcQ","b1WWpKEPdT4"];
//
//
var idsAmount = ids.length;
for (var i in idsAmount) {
document.write("<iframe src='"https://www.youtube.com/embed/" + ids[i] + "?modestbranding=1&rel=0&autoplay=1'" /> <br />");
};
</script>
控制台中没有错误。知道为什么什么都没发生吗?
编辑:这是我最终的工作代码:
<script>
// Replace line below with video id(s), e.g. ["dQw4w9WgXcQ"] or ["dQw4w9WgXcQ","b1WWpKEPdT4"]
var ids = ["dQw4w9WgXcQ","b1WWpKEPdT4"];
//
if (ids.length == 1) {
document.write("<iframe width='"640px'" height='"360px'" src='"https://www.youtube.com/embed/" + ids + "?modestbranding=1&rel=0&autoplay=1'" frameborder='"0'" allowfullscreen></iframe>");
document.write("<br />");
}
else {
for (var i = 0; i < ids.length; i++) {
document.write("<iframe width='"640px'" height='"360px'" src='"https://www.youtube.com/embed/" + ids[i] + "?modestbranding=1&rel=0'" frameborder='"0'" allowfullscreen></iframe>");
document.write("<br /> <br />");
};
};
</script>
首先,您正在转义两个不需要转义的引号(用于连接),而且,循环是错误的,应该如下所示:
<script>
// Replace line below with video id(s), e.g. ["dQw4w9WgXcQ"] or ["dQw4w9WgXcQ","b1WWpKEPdT4"]
var ids = ["dQw4w9WgXcQ","b1WWpKEPdT4"];
//
//
for (var i = 0; i < ids.length; i++) {
document.write("<iframe src='"https://www.youtube.com/embed/" + ids[i] + "?modestbranding=1&rel=0&autoplay=1'" /> <br />");
};
</script>
而不是使用document.write()
和for...
循环,我建议简单地迭代数组本身,使用Array.prototype.map()
,创建一个HTML节点数组,它本身被迭代到将这些节点追加到文档片段,这反过来可以追加到指定的DOM节点:
// Array of video ids:
var ids = ["dQw4w9WgXcQ", "b1WWpKEPdT4"],
// creating an <iframe> element:
iframeElement = document.createElement('iframe'),
// creating a document fragment:
fragment = document.createDocumentFragment(),
// an unitialised (undefined) variable for later use:
clone,
// iterating over the array of ids, creating a new
// array using Array.prototype.map():
html = ids.map(function(currentID) {
// in the anonymous function the first
// argument (here: 'currentID') is the
// current array-element of the array
// over which we're iterating.
// cloning the <iframe>:
var clone = iframeElement.cloneNode();
// setting the src property of the cloned
// <iframe>:
clone.src = 'https://www.youtube.com/embed/' + currentID + '?modestbranding=1&rel=0&autoplay=0';
// Note that 'autoplay' has been set to
// to '0' on principle.
// returning the modified clone to the array
// we're creating:
return clone;
// iterating over the array returned from
// Array.prototype.map() and appending each
// array-element to the document fragment:
}).forEach(function(iframe) {
fragment.appendChild(iframe);
});
// appending the document fragment to the body element:
document.body.appendChild(fragment);
External JS Fiddle demo.
引用:
-
Array.prototype.forEach()
. -
Array.prototype.map()
. -
document.createDocumentFragment()
. -
document.createElement()
. -
HTMLIFrameElement
. -
Node.appendChild
.
相关文章:
- 可以't从Youtube IFrame更改焦点
- Youtube iframe api未在YouTubeIframeAPIReady上触发
- 使用YouTube iFrame API的IE中的JavaScript问题
- 多个youtube iframe播放此内容
- Browserify breaks Youtube iFrame API
- 使用 javascript 收听 youtube iframe 事件
- Iframe|youtube Iframe上的移动后退按钮
- Youtube iframe API-重置音量
- 简单的Youtube iframe API就在文档中,我做错了什么
- Displaynone属性停止在Firefox中播放youtube iframe
- Youtube IFrame API来源不明
- YouTube iframe API and WordPress
- Youtube iFrame API pauseVideo在UIWebView中未定义
- 带有主干的Youtube IFrame API
- Youtube iFrame API无法控制播放器
- Youtube iframe没有响应通过上图传递的指针事件的点击:none
- 在页面加载后插入 YouTube iframe
- 没有全屏的iPhone 上的 YouTube Iframe API
- Youtube iFrame API 'YT is undefined'
- 如何从数组获取链接以在youtube Iframe API中播放