JS的play()在jQuery中?

Bor*_*Bor 3 javascript audio jquery

在jQuery中是否有JS的play()或pause()方法?我正在寻找jquery解决方案,但最好没有其他插件.

$('#play').click(function() { 
    function play() {  
      //  the same in jquery ?  
        document.getElementById('demo').play();
        document.getElementById('demo').volume = 1;       
    }
    function play_pause() { 
       //  the same in jquery ?
        document.getElementById('demo').pause();
        document.getElementById('demo').currentTime = 0;        
    }
    if ( $(this).val() === "play" ) {
       $(this).val("pause"); 
       play();
    } else {
       $(this).val("play");
      pause();
    } 
});
Run Code Online (Sandbox Code Playgroud)

我需要最简单的解决方案,而不是大花哨的插件.

Cod*_*gue 5

您可以像这样从jQuery获取原始HTML元素;

$("#demo")[0].play();
Run Code Online (Sandbox Code Playgroud)

您可能还需要检查demo元素是否确实存在:

if($("#demo").length) $("#demo")[0].play();
Run Code Online (Sandbox Code Playgroud)

所以在你的例子中,你会有:

$('#play').click(function() { 
    if ( $(this).val() === "play" ) {
       $(this).val("pause");
       $("#demo")[0].play();
       $("#demo")[0].volume = 1;
    } else {
       $(this).val("play")[0].pause();
       $("#demo")[0].pause();
       $("#demo")[0].currentTime = 0; 
    } 
});
Run Code Online (Sandbox Code Playgroud)