[英]video tag event listener
我正在开发使用视频标签的视频播放器。 我从播放器中获得的信息是,当有人单击视频时,如果正在播放,则停止播放;如果未单击视频,则停止播放,然后开始播放
下面是我的代码:
window.onlaod=init();
function init(){
BarLenght= 900;
video = document.getElementById('vid');
video.addEventListener('click', playOrPause , false);
alert("hello");
}
function playOrPause(){
if((!video.paused) && (!video.ended))
{
video.pause();
}
else{
if((video.paused) && (!video.ended))
{
video.play();
}
else{
video.currentTime=0;
video.play();
}
}
}
但它不起作用,谁能知道为什么它不起作用..?
Window.onload拼写错误。 您可能想尝试一下。
很难准确指出错误的出在哪里,但是代码中肯定有一些事情会引起问题。 你onload
是拼写错误。 另外,您的video
变量是在init中分配的。 您尚未为其声明全局变量,因此可能会导致此问题。 根据JS的设计,不带var
前缀的变量将被假定为全局变量,但您永远不应假定其为真。 始终在打算使用它们的范围内声明变量。 以下可能会达到目的...
var video = null;
function init(){
BarLength = 900; //this was misspelled, also I'm not sure what it refers to...
video = document.getElementById('vid');
video.addEventListener('click', playOrPause , false);
alert("hello");
}
function playOrPause(){
if(!video.paused && !video.ended)
{
video.pause();
} else{
if(video.paused && !video.ended) {
video.play();
} else{
video.currentTime=0;
video.play();
}
}
}
window.addEventListener('load', init);
<!DOCTYPE html>
<html>
<body>
<button onclick="play()" type="button">Play</button>
<button onclick="pause()" type="button">Pause</button>
<button onclick="stop()" type="button">Stop</button><br>
<video id="vid" width="" height="">
<source src="/video.mp4" type="video/mp4">
Error!
</video>
<script>
var vid = document.getElementById("vid");
function play() {
vid.play();
}
function pause() {
vid.pause();
}
function stop() {
vid.src="/video.mp4";
}
</script>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.