我尝试在网站上嵌入YouTube视频并将其静音。
我已经找到了这个主题:How do you mute an embedded Youtube player?
所以我尝试了答案,这是有效的:https://jsfiddle.net/Jonathan_Ironman/BFDKS/9/
但是当我在jsfiddle之外尝试相同的代码时,javascipt代码不再执行了。
源代码
<html>
<head>
<script type="text/javascript">
var tag = document.createElement('script');
tag.src = "//www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady()
{
player = new YT.Player('ytplayer',
{
events:
{
'onReady': onPlayerReady
}
});
}
function onPlayerReady()
{
player.playVideo();
// Mute!
player.mute();
}
</script>
<iframe id="ytplayer" type="text/html" style="position:absolute; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden; z-index:0;" src="https://www.youtube-nocookie.com/embed/DjxwLr6TjHs?playlist=DjxwLr6TjHs&enablejsapi=1&border=0&start=60&autoplay=1&showinfo=0&controls=0&disablekb=1&cc_load_policy=1&iv_load_policy=3&rel=0&loop=1" frameborder="0"></iframe>
</head>
<body>
</body>
</html>
有什么想法吗?
答案 0 :(得分:3)
让你的第一行js看起来像这样(复制和粘贴)
tag.src = "http://www.youtube.com/iframe_api";
否则(本地)它将指向一个文件..
也许您只需添加一个控制台输出来检查api是否正确加载:)