更改YouTube播放器的视频源

当用户单击图像链接(另一个视频的链接)时,YouTube播放器会在重新加载页面后加载此视频。我不知道为什么我必须在图像链接上单击两次(= 2刷新)才能看到youtube播放器随新视频而变化。为什么?我只想当用户在图片链接上单击一次时,YouTube播放器会更改视频。

function myFunction() { /* found the index of the current videoId in the URL*/
  var lecteur = document.getElementById("lecteur").src;
  var lecteurVideoId = lecteur.substring(lecteur.lastIndexOf("/") + 1);
  /*Found the index of the videoId in the image link */
  var urlComplete = document.getElementById("video1").href;
  var videoUrl = urlComplete.substring(urlComplete.lastIndexOf("#") + 1);

  lecteur = lecteur.replace(lecteurVideoId,videoUrl);
  document.getElementById("lecteur").src = lecteur;
}
<!--youtube player-->
<iframe id="lecteur" width="100%" height="600" src="https://www.youtube.com/embed/tje3EuE-IbE" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

<!--Image link  with the youtube video ID in the href -->
<a id="video1" href="multimedia.html#b62zpFHcpgw" onclick="myFunction()"><img src="https://via.placeholder.com/350x150" alt="" id="" /></a>

rockywell 回答:更改YouTube播放器的视频源

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3078791.html

大家都在问