我尝试将视频ID 8IYzyTYucKQ
动态更改为WKV_z36pVAk
,但不会发生变化。
我认为图书馆只会在页面重新加载时工作,所以当我点击按钮时新的ID会改变,但视频会播放较旧的Id,当我再次重新加载视频时,我将重置为默认值。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>About</title>
<script type="text/javascript">
function val1(val)
{
location.reload();
document.getElementsByTagName("div")[0].setAttribute("data-video", val);
//document.getElementById("youtube-audio").data-video=val;
//document.getElementById("p1").innerHTML=val;
alert(val);
}
</script>
</head>
<body>
<h3 style="color:#0066dd">Hello</h3>
<center>
<div data-video="8IYzyTYucKQ"
data-autoplay="0"
data-loop="1"
id="youtube-audio">
</div>
</center>
<button onclick="val1('WKV_z36pVAk')">change</button>
</body>
<script src="https://www.youtube.com/iframe_api"></script>
<script src="https://cdn.rawgit.com/labnol/files/master/yt.js"></script>
</html>
答案 0 :(得分:1)
You're reloading the page after you change the data attribute using :
location.reload();
What returns the attribute to the default value when the page refreshed and ready again.
Simple example using the API :
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://www.youtube.com/iframe_api"></script>
</head>
<body>
<h3 style="color:#0066dd">Hello</h3>
<button id="change_video">change</button>
<div id="player"></div>
<script>
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '360',
width: '640',
videoId: 'M7lc1UVf-VE'
});
}
$('#change_video').on('click', function(){
player.loadVideoById('WKV_z36pVAk');
})
</script>
</body>
</html>
Hope this helps.