点击yt视频时加载js函数



点击Yt视频后,是否可以执行Java Script函数?

我的代码如下:

<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

最新更新