如何调用javascript函数,当用户单击视频播放的帧时

How to call javascript function, when the user click on the frame in which the video play

本文关键字:视频 单击 用户 播放 何调用 调用 函数 javascript      更新时间:2023-09-26
<script>
    function myFunction() {
        alert("hello");
    }
</script> 
<iframe width="100%" height="315" src="https://www.youtube.com/embed/AGM0ibP1MRc" onclick="myFunction()"></iframe>

.请任何人。帮助

一种解决方案是覆盖一个透明的div 并向该div 添加一个点击事件。但是您将无法单击 iframe。

如果要使视频可点击,请添加此内容

document.getElementById("overlay").style.display = "none";

工作片段:

function myFunction() {
  alert("Now you can play the video");
  document.getElementById("overlay").style.display = "none";  //makes the iframe clickable 
}
.container {
  position:relative;
}
.overlay {
  top:0;
  left:0;
  width:100%;
  height:100%;
  position:absolute;
}
<div class="container">
  <iframe width="100%" height="315" src="https://www.youtube.com/embed/AGM0ibP1MRc"></iframe>
  <div class="overlay" id="overlay" onclick="myFunction()"></div>
</div>