暂停多个 HTML5 视频
我在 http://ghostpool.com/wordpress/phideo 的滑块中有多个 HTML5 视频我单击任何滑块按钮,第一个视频将使用以下 onclick
函数暂停。
<a href="#" onclick="pausePlayer();">LINK</a>
var htmlPlayer = document.getElementsByTagName('video')[0];
function pausePlayer() {
htmlPlayer.pause();
}
但是,单击滑块按钮时第二个视频不会暂停。我认为上面的 Javascript 不适用于多个视频?
编辑:此外,如果单击视频元素本身,我想切换播放和暂停功能。我正在使用这段代码,但它会立即播放和停止所有视频。我认为我没有正确循环它:
function togglePlayer() {
for(var i = 0; i < htmlPlayer.length; i++){
if ( htmlPlayer[i].paused || htmlPlayer[i].ended ) {
if ( htmlPlayer[i].ended ) { htmlPlayer[i].currentTime = 0; }
htmlPlayer[i].play();
} else {
htmlPlayer[i].pause();
}
}
}
I have multiple HTML5 videos in a slider at http://ghostpool.com/wordpress/phideo and when I click on any of the slider buttons the first video is paused using the following onclick
function.
<a href="#" onclick="pausePlayer();">LINK</a>
var htmlPlayer = document.getElementsByTagName('video')[0];
function pausePlayer() {
htmlPlayer.pause();
}
However the second video does not pause when the slider buttons are clicked. I assume the Javascript above does not work with multiple videos?
EDIT: Additionally, if clicking on the video element itself I want to toggle a play and pause function. I"m using this code, but it plays and stops all videos at once. I assume I'm not looping it correctly:
function togglePlayer() {
for(var i = 0; i < htmlPlayer.length; i++){
if ( htmlPlayer[i].paused || htmlPlayer[i].ended ) {
if ( htmlPlayer[i].ended ) { htmlPlayer[i].currentTime = 0; }
htmlPlayer[i].play();
} else {
htmlPlayer[i].pause();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这显然行不通,因为您只能获取
getElementsByTagName
返回的数组中的第一个元素。[0]
部分访问数组中的第一个元素。您想要做的是用循环遍历所有这些,如下所示:编辑
关于您的第二个问题 - 您可以尝试使用它:
this
in a事件处理程序应该引用调用它的元素。This obviously won't work because you are only getting the first element in the array returned by
getElementsByTagName
. The[0]
part accesses the first element in the array. What you would want to do is to iterate through all of them with a loop, like this:Edit
Regarding your second question - You can try using this instead:
this
in a event handler should refer to the element that called it.