gpt4 book ai didi

单击另一个 `

转载 作者:行者123 更新时间:2023-11-30 20:38:10 38 4
gpt4 key购买 nike

我的问题是当我显示 <video> 时通过单击第一个按钮并在不切换第一个按钮的情况下单击另一个按钮,两个视频一起显示,我该如何解决?

当我点击另一个按钮时,我希望第一个按钮自动关闭和隐藏。

JavaScript 代码:

<script> 
function display(id) {
var vid = document.getElementById(id);
if (vid.style.display == 'block') {
vid.style.display = 'none';
vid.pause();
} else {
vid.style.display = 'block';
}
}
</script>

HTML代码:

<section class="vid">
<h3>Double click on the buttons to display the videos:</h3>
<div><a onclick="display('ch1');"><span>Video tutorual Ch1</span></a></div>
<div><a onclick="display('ch2');"><span>Video tutorial Ch2</span></a></div>
<div><a onclick="display('ch3');"><span>Video tutorial Ch3</span></a></div>
<div><a onclick="display('ch4');"><span>Video tutorial Ch4</span></a></div>
<div><a onclick="display('ch5');"><span>Video tutorial Ch5</span></a></div>
<div><a onclick="display('ch6');"><span>Video tutorial Ch6</span></a></div>
</section>
<video controls="" height="600px" id="ch1" src="V.mp4" width="940px"></video>
<video controls="" height="600px" id="ch2" src="V.mp4" width="940px"></video>
<video controls="" height="600px" id="ch3" src="V.mp4" width="940px"></video>
<video controls="" height="600px" id="ch4" src="V.mp4" width="940px"></video>
<video controls="" height="600px" id="ch5" src="V.mp4" width="940px"></video>
<video controls="" height="600px" id="ch6" src="V.mp4" width="940px"></video>

最佳答案

在你的点击函数中,你需要先停止所有的视频,然后显示并播放用户通过点击函数参数选择的一个视频。示例代码如下,可以帮助进一步增强。您可以调用 hideAndStopAll() 方法在页面加载时停止并隐藏它们。

   function display(id) {
hideAndStopAll()
var vid = document.getElementById(id);
vid.style.display = 'block';
vid.play();

}

var ids= ['ch1','ch2','ch3','ch4','ch5','ch6']

function hideAndStopAll(){

for(i=0;i<ids.length;i++){
var video = document.getElementById(ids[i]);
video.pause();
video.style.display='none'
}

}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>

<div> <a onclick="display('ch1');"> <span>Video tutorual Ch1</span> </a> </div>
<div> <a onclick="display('ch2');"> <span>Video tutorial Ch2</span> </a> </div>
<div> <a onclick="display('ch3');"> <span>Video tutorial Ch3</span> </a> </div>
<div> <a onclick="display('ch4');"> <span>Video tutorial Ch4</span> </a> </div>
<div> <a onclick="display('ch5');"> <span>Video tutorial Ch5</span> </a> </div>
<div> <a onclick="display('ch6');"> <span>Video tutorial Ch6</span> </a> </div>


<video id="ch1" src="http://clips.vorwaerts-gmbh.de/VfE_html5.mp4" controls width="940px" height="600px"> </video>
<video id="ch2" src="http://clips.vorwaerts-gmbh.de/VfE_html5.mp4" controls width="940px" height="600px"> </video>
<video id="ch3" src="http://clips.vorwaerts-gmbh.de/VfE_html5.mp4" controls width="940px" height="600px"> </video>
<video id="ch4" src="http://clips.vorwaerts-gmbh.de/VfE_html5.mp4" controls width="940px" height="600px"> </video>
<video id="ch5" src="http://clips.vorwaerts-gmbh.de/VfE_html5.mp4" controls width="940px" height="600px"> </video>
<video id="ch6" src="http://clips.vorwaerts-gmbh.de/VfE_html5.mp4" controls width="940px" height="600px"> </video>

</body>
</html>

关于单击另一个 `<video>` 时 JavaScript 关闭/隐藏视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49591554/

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