gpt4 book ai didi

javascript - 单击 yt-video 时加载 js 功能?

转载 作者:行者123 更新时间:2023-12-04 08:52:09 27 4
gpt4 key购买 nike

单击开始播放 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 视频时没有任何 react 。有人可以帮我解决这个问题。我很感激答案。

最佳答案

<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将在视频开始播放时调用。

关于javascript - 单击 yt-video 时加载 js 功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64049418/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com