随机youtube链接在嵌入式视频中播放

时间:2016-10-20 10:19:44

标签: javascript jquery

$("#video").attr('src', idea(ads));
$(".heading").text(idea(ads));
var ads = ["https://youtu.be/nSZtxXNjmLA", "https://www.youtube.com/watch?v=D2pOkm4opVU"]; 
function idea(el) {
    return el[Math.floor(Math.random() * el.length)];
}

所以我使用函数的想法随机化它。 iframe的视频ID为。

1 个答案:

答案 0 :(得分:0)

1 - 将var ads移至第一行

2 - 在你的html https://learn.jquery.com/about-jquery/how-jquery-works/

中链接jquery
  <script src="https://code.jquery.com/jquery-git.js"></script>

&#13;
&#13;
var ads = [
  "youtu.be/nSZtxXNjmLA",
  "youtube.com/watch?v=D2pOkm4opVU",
  "youtu.be/WElPhX5Pb6I",
  "youtu.be/78mNZeDaMtk"
];

$("#video").attr('src', idea(ads));

$(".heading").text(idea(ads)); 

function idea(el) { 
  return el[Math.floor(Math.random() * el.length)]; 
}
&#13;
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Demo</title>
</head>

<body>
  <h1 class="heading"></h1>
  <video src=""></video>
  <script src="https://code.jquery.com/jquery-git.js"></script>
</body>
</html>
&#13;
&#13;
&#13;