基本上,我正在尝试从我拥有的这种数据结构中填充嵌套在section
标记中的iframe。
HTML:
<div id="container-fluid--secondary" class="container-fluid container-fluid--secondary">
<section class="a">
<header>...more stuff that's not important</header>
<iframe src="" width="790" height="444" frameborder="0" webkitallowfullscreen
mozallowfullscreen allowfullscreen>
</iframe>
<div class="video-container">
<img class="video-container__thumbnail" src="https://via.placeholder.com/250x141.png?text=Video Thumbnail 250px x 141px" alt="Video thumbnail">
<img class="video-container__thumbnail" src="https://via.placeholder.com/250x141.png?text=Video Thumbnail 250px x 141px"
alt="Video thumbnail">
<img class="video-container__thumbnail" src="https://via.placeholder.com/250x141.png?text=Video Thumbnail 250px x 141px"
alt="Video thumbnail">
</div>
</section>
<section class="b">
<header>...more stuff that's not important</header>
<iframe src="" width="790" height="444" frameborder="0" webkitallowfullscreen
mozallowfullscreen allowfullscreen>
</iframe>
<div class="video-container">
<img class="video-container__thumbnail" src="https://via.placeholder.com/250x141.png?text=Video Thumbnail 250px x 141px"
alt="Video thumbnail">
<img class="video-container__thumbnail" src="https://via.placeholder.com/250x141.png?text=Video Thumbnail 250px x 141px"
alt="Video thumbnail">
<img class="video-container__thumbnail" src="https://via.placeholder.com/250x141.png?text=Video Thumbnail 250px x 141px"
alt="Video thumbnail">
</div>
</section>
</div>
JS:
var videos = {
'a': [
{
'name': 'a',
'src': 'https://foo/video/',
'thumb': 'images/foo.png'
},
{
'name': 'b',
'src': 'https://bar/video/',
'thumb': 'images/bar-10.png'
},
{
'name': 'c',
'src': 'https://baz/video/',
'thumb': 'images/baz.png'
}
],
'b': [
{
'name': 'a',
'src': 'https://fizz/video/',
'thumb': 'images/fiz.png'
},
{
'name': 'b',
'src': 'https://buzz/video/',
'thumb': 'images/buzz.png'
},
{
'name': 'c',
'src': 'https://blargh/video/',
'thumb': 'images/blargh.png'
}
]
},
var containerFluidSecondary = document.getElementById('container-fluid--secondary');
var videoNames = Object.keys(videos);
containerFluidSecondary.querySelectorAll('section').forEach(function (section, i) {
if (section.className == videoNames[i]) {
var videoContainer = [...section.getElementsByClassName('video-container')[0].children];
var vids = videos[section.className];
videoContainer.forEach(function (img, i) {
img.src = vids[i].thumb
img.addEventListener('click', function(){
console.log(section.getElementsByTagName('iframe').src = vids[i].src);
},false);
})
}
})
这会使用新的源注销iframe,但是为什么不将其填充到iframe中?
img.addEventListener('click', function(){
console.log(section.getElementsByTagName('iframe').src = vids[i].src);
},false);