Javascript random youtube video autoplay

人盡茶涼 提交于 2019-12-01 12:16:49

问题


I need to display a random youtube video from a selection when the page loads. I found the question below very helpful however I can't figure out how to get it to autoplay. I've tried adding

&autoplay=1

after "videos[index]" but I can't get it to work / don't know if I am putting it in the wrong place. Any help would be much appreciated.

Trying to display a random video on page load

$(document).ready(function() {

var videos = [
'pRpvdcjkT3k',
'Te4wx4jtiEA',
'efTj6UYzvk4'
];

var index=Math.floor(Math.random() * videos.length);
var html='<div id="video"><h3>Random Video</h3><iframe width="720" height="480" src="http://www.youtube.com/embed/ ' + videos[index] + ' + "&autoplay=1" " frameborder="0" allowfullscreen></iframe></div>';
document.write(html);

});//-->

回答1:


$(document).ready(function() {

var videos = [
'pRpvdcjkT3k',
'Te4wx4jtiEA',
'efTj6UYzvk4'
];

var index=Math.floor(Math.random() * videos.length);
var html='<div id="video"><h3>Random Video</h3><iframe width="720" height="480"   src="http://www.youtube.com/embed/' + videos[index] + '?autoplay=1" frameborder="0" allowfullscreen></iframe></div>';
document.write(html);

});

'?' instead of '&'



来源:https://stackoverflow.com/questions/22128689/javascript-random-youtube-video-autoplay

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!