简体   繁体   English

循环JavaScript

[英]Looping JavaScript

I have been searching Stackoverflow and Google, and tried everything I clould think of to figure out how to loop through an html file with javascript and find all of the video files, and their unique ID, but I cant seem to get it right. 我一直在搜索Stackoverflow和Google,并尝试了我可能想到的所有方法,以弄清楚如何使用javascript循环遍历html文件并找到所有视频文件及其唯一ID,但我似乎无法正确解决。 I have not been coding in several years, so I am very rusty and most of my experience is with back end code. 几年来我一直没有编码,所以我很生锈,我的大部分经验是关于后端代码的。 I have very little experience with DOM and Javascipt, I am sure what ever I have tried, I have probably screwed up the syntax/semantics. 我对DOM和Javascipt的经验很少,我敢肯定,无论尝试过什么,我都可能搞砸了语法/语义。

What the code does: 代码的作用是:

The Javascript makes videos pop up and play in a modal window, however since it is hard coded, I can only input one unique ID, and when you click on each of the videos on the web page, of course, they play the same video. Javascript使视频弹出并在模式窗口中播放,但是由于它是经过硬编码的,所以我只能输入一个唯一的ID,并且当您单击网页上的每个视频时,它们当然会播放相同的视频。

Hope someone can help, this is a personal project for my game collection and the only thing I have left to complete is this last piece of code. 希望有人能帮忙,这是我的游戏收藏的个人项目,而我剩下要做的唯一一件事就是这最后的代码。 Thanks in advance for any assistance. 在此先感谢您的协助。

 window.document.onkeydown = function(e) { if (!e) { e = event; } if (e.keyCode == 27) { lightbox_close(); } } function lightbox_open() { var lightBoxVideo = document.getElementById("Some-ID-Variable"); window.scrollTo(0, 0); document.getElementById('light').style.display = 'block'; document.getElementById('fade').style.display = 'block'; lightBoxVideo.play(); } function lightbox_close() { var lightBoxVideo = document.getElementById("Some-ID-Variable"); document.getElementById('light').style.display = 'none'; document.getElementById('fade').style.display = 'none'; lightBoxVideo.pause(); } 
 <!--Here is the HTML I have, I am only showing 2 game titles in the example, but this code repeats for every game in my collection:--> <div class="containers"> <!--Begin .container--> <div id="light"> <a class="boxclose" id="boxclose" onclick="lightbox_close();"></a> <video id="MyVideo" width="600" controls> <source src="videos/game-title-1-trailer.mp4" type="video/mp4"> </video> </div> <div id="fade" onClick="lightbox_close();"></div> <div> <a href="#" onclick="lightbox_open();"><img class="title-image" src="images/placeholder.jpg" alt="Some Title"></a> <br> <a href="#" onclick="lightbox_open();"><span class="title-text">Game Title 1</span></a> <br> <ul> <li>Comfort: Intense</li> <li>Learning Curve: Intermediate</li> </ul> <br> <p class="desc-text">The game decription goes here. The game decription goes here. The game decription goes here. The game decription goes here.</p> <br> </div> </div> <!--End .container--> <div class="containers"> <!--Begin .container--> <div id="light"> <a class="boxclose" id="boxclose" onclick="lightbox_close();"></a> <video id="MyVideo" width="600" controls> <source src="videos/game-title-trailer-2.mp4" type="video/mp4"> </video> </div> <div id="fade" onClick="lightbox_close();"></div> <div> <a href="#" onclick="lightbox_open();"><img class="title-image" src="images/placeholder.jpg" alt="Some Title"></a> <br> <a href="#" onclick="lightbox_open();"><span class="title-text">Game Title 2</span></a> <br> <ul> <li>Comfort: Intense</li> <li>Learning Curve: Intermediate</li> </ul> <br> <p class="desc-text">The game decription goes here. The game decription goes here. The game decription goes here. The game decription goes here.</p> <br> </div> </div> 

Like James said, document.querySelectorAll('video') will get every video tag on the page, but if I understand your question correctly, you want to find a specific ID and play it when it's chosen. 就像James所说的那样, document.querySelectorAll('video')将获得页面上的每个视频标签,但是如果我正确理解了您的问题,则希望找到一个特定的ID并在选择它时进行播放。 Looping is probably an inefficient way to do that, especially if you have many videos. 循环可能是一种低效的方法,尤其是当您有很多视频时。

Perhaps you can try putting a variable in your onclick, like onclick="lightbox_open('1')" , then in that function you can do: 也许您可以尝试将变量放入onclick中,例如onclick="lightbox_open('1')" ,然后可以在该函数中执行以下操作:

function lightbox_open(idNum) {
  var lightBoxVideo = document.getElementById("Some-ID-Variable" + idNum);
  //...
}

Then if you're building more possible videos from the backend, you can just increment that variable when you set each element's onclick attribute. 然后,如果您要从后端构建更多可能的视频,则只需在设置每个元素的onclick属性时递增该变量即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM