使用jQuery控制HTML5 元素

时间:2021-02-07 15:21:38

I'm trying to use jQuery to control an HTML5 audio element, but I'm no genius with JS. What I want is for the player to start on page load, which I've done, but when the play button is clicked, I want to check whether or not the audio is playing. If it's playing when clicked: Stop the audio. If it's not playing when clicked: Play the audio.

我正在尝试使用jQuery来控制HTML5音频元素,但我不是JS的天才。我想要的是让玩家从页面加载开始,我已经完成了,但是当点击播放按钮时,我想检查音频是否正在播放。如果单击时正在播放:停止音频。如果单击时没有播放:播放音频。

If you could help, it'd be much appreciated.

如果你能提供帮助,我们将不胜感激。

Source here: http://www.julake.co.uk/media/loader.php?page=contact

来源:http://www.julake.co.uk/media/loader.php?page = contact

Many thanks,

非常感谢,

3 个解决方案

#1


12  

you should use a single play/pause toggle button in which you need to check if your audio is paused or not

您应该使用单个播放/暂停切换按钮,您需要在其中检查音频是否暂停

var audioplayer = document.getElementById("audio-player");
$("#play-bt").click(function(){
    if (audioplayer.paused) {
       audioplayer.play();
    }   
    else {
       audioplayer.pause();
    }
    $(this).toggleClass('pause');  /* style your toggle button according to 
                                      the current state */
})

#2


2  

var audio = new Audio("http://www.w3schools.com/html5/song.ogg"); //or you can get it with getelementbyid

audio.addEventListener('canplay', function() {
//code, when audio can play
audio.play(); //this function will start the music
})

with audio.play() function you can start it. You don't need JQuery

使用audio.play()函数可以启动它。你不需要JQuery

#3


1  

If you wish to use a jquery selector or have a jquery selector already, you can add [0] to get the native dom element.

如果你想使用jquery选择器或者已经有一个jquery选择器,你可以添加[0]来获取本机dom元素。

So an example that actually uses jquery would be.

所以实际使用jquery的例子就是。

$('audio')[0].pause()

$( '音频')[0] .pause()

#1


12  

you should use a single play/pause toggle button in which you need to check if your audio is paused or not

您应该使用单个播放/暂停切换按钮,您需要在其中检查音频是否暂停

var audioplayer = document.getElementById("audio-player");
$("#play-bt").click(function(){
    if (audioplayer.paused) {
       audioplayer.play();
    }   
    else {
       audioplayer.pause();
    }
    $(this).toggleClass('pause');  /* style your toggle button according to 
                                      the current state */
})

#2


2  

var audio = new Audio("http://www.w3schools.com/html5/song.ogg"); //or you can get it with getelementbyid

audio.addEventListener('canplay', function() {
//code, when audio can play
audio.play(); //this function will start the music
})

with audio.play() function you can start it. You don't need JQuery

使用audio.play()函数可以启动它。你不需要JQuery

#3


1  

If you wish to use a jquery selector or have a jquery selector already, you can add [0] to get the native dom element.

如果你想使用jquery选择器或者已经有一个jquery选择器,你可以添加[0]来获取本机dom元素。

So an example that actually uses jquery would be.

所以实际使用jquery的例子就是。

$('audio')[0].pause()

$( '音频')[0] .pause()