HTML 从数组中为 SRC 路径随机加载 pathID

HTML load pathID from array randomly for SRC path

我正在尝试让网页从数组中随机加载 youtube 视频,这就是我想告诉你的

String[] pathID = {"ONjesT18bVQ","ONjesT18bVQ","ONjesT18bVQ"};

<div style="position: fixed; z-index: -99; width: 100%; height: 100%">
<iframe frameborder="0" height="100%" width="100%"
src="https://youtube.com/embed/"+ pathID[get random number here]+"autoplay=1&controls=0&showinfo=0&autohide=1">
</iframe>
</div>

这就是我想要的样子,pathID 是从数组加载的,但我不知道如何在 HTMl 中执行此操作,我在 html[ 中没有太多经验=12=]

谢谢

只需将 iframesrc 设置为随机的 youtube id:

JS

var paths = ["ONjesT18bVQ","ONjesT18bVQ","ONjesT18bVQ"]; //array of ids

var item = paths[Math.floor(Math.random()*paths.length)]; //random

document.getElementById("video").src = "https://youtube.com/embed/"+item+"?autoplay=1&controls=0&showinfo=0&autohide=1" //set id

HTML

<div style="position: fixed; z-index: -99; width: 100%; height: 100%">
   <iframe id="video" frameborder="0" height="100%" width="100%"></iframe>
 </div>

FIDDLE

这不是 JS :)

<script type="text/javascript">
var videoLinks = ["ONjesT18bVQ","ONjesT18bVQ","ONjesT18bVQ"],
    url = '',
    l = videoLinks.length,
    headerBlock = '<div style="position: fixed; z-index: -99; width: 100%; height: 100%"><iframe frameborder="0" height="100%" width="100%"src="https://youtube.com/embed/',
    footerBlock = '"autoplay=1&controls=0&showinfo=0&autohide=1"></iframe></div>',
    i;

function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

for (i=0; i<l; i++){
  document.writeln( headerBlock + videoLinks[getRandomInt(0,l)] + footerBlock );
}
</script>