微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

点击yt视频时加载js函数?

如何解决点击yt视频时加载js函数?

一旦点击开始观看Yt视频,是否可以执行Java脚本功能

我的代码如下:

<html>
<head><title>onclick event demo</title>
</head>
<body>
    <h3>onclick event demo</h3>
    <p>Fires when the element is clicked.</p>
    
        <embed
        onclick="VideoStart()"
        src="https://www.youtube.com/embed/qK9v-VFfdY0?start=478" style="border:1px solid;" 
        >
        
        
</body>
</html>


<script>
function VideoStart(){
    ***code executed when clicking on the Yt-Video***
}

</script>

但是由于某些原因,当我单击鼠标来启动或停止YT视频时,什么也没有发生。有人可以帮我这个忙。我希望得到一个答案。

解决方法

<html>
  <body>
    <!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
    <div id="player"></div>

    <script>
      // 2. This code loads the IFrame Player API code asynchronously.
      var tag = document.createElement('script');

      tag.src = "https://www.youtube.com/iframe_api";
      var firstScriptTag = document.getElementsByTagName('script')[0];
      firstScriptTag.parentNode.insertBefore(tag,firstScriptTag);

      // 3. This function creates an <iframe> (and YouTube player)
      //    after the API code downloads.
      var player;
      function onYouTubeIframeAPIReady() {
        player = new YT.Player('player',{
          height: '360',width: '640',videoId: 'M7lc1UVf-VE',events: {
            'onReady': onPlayerReady,'onStateChange': onPlayerStateChange
          }
        });
      }

      // 4. The API will call this function when the video player is ready.
      function onPlayerReady(event) {
        //event.target.playVideo();
      }

      // 5. The API calls this function when the player's state changes.
      //    The function indicates that when playing a video (state=1),//    the player should play for six seconds and then stop.
      function onPlayerStateChange(event) {
        if (event.data == YT.PlayerState.PLAYING) {
          myFunction();
        }
      }
      function stopVideo() {
        player.stopVideo();
      }
      function myFunction(){
        alert("Hello world");
      }
    </script>
  </body

这应该有效。只需更改您所需的值即可。视频开始播放时,将调用myFunction

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。