Stop YouTube video within iFrame on external Button click

前端 未结 3 2045
予麋鹿
予麋鹿 2021-02-12 17:43

I know there are similar questions to this, but I haven\'t quite been able to get it to work.

This code shows a YouTube video which is within an iframe. When a button (

3条回答
  •  死守一世寂寞
    2021-02-12 18:14

    Well, you just need to change the source (src) attribute value of iframe tag. It's that simple! :)

    With javascript, you can do it as follows:

    var stopButton = document.getElementById('video-close-button-id');
    
    stopButton.onclick = function() {
      var myPlayer = document.getElementById("youtube-player-id"); 
      myPlayer.setAttribute("src", " ");
    }; 
    

    Happy coding.

提交回复
热议问题