无法选择视频

时间:2018-10-13 22:37:21

标签: javascript jquery html5

我正在尝试使视频静音/取消静音。但是首先,看来我无法选择我的视频。尝试过教程示例和类似问题的答案,但它们都指向选择视频的相同方法。请在这里提出问题。谢谢。

<!-- HTML -->
<button id="enableMute" onclick="enableMute()" type="button">Mute</button>
<button id="disableMute" type="button">Enable</button>
<button id="checkMute" type="button">Status</button><br>

<video id="myVideo" width="320" height="176" autoplay loop muted>
    <source src="video/trailer.mp4" type="video/mp4">
    Your browser does not support HTML5 video.
</video>

<!-- JS -->
$(function() {
    // seems not being selected. Tried without hash and same result. 
    var vid = $("#myVideo"); 

    //alert works but clearly nothing happening with mute controls
    $("#enableMute").click(function(){
        alert("enableMute");
        vid.muted = true;
        vid.volume = 1.0;
    });

    //alert works but clearly nothing happening with mute controls
    $("#disableMute").click(function(){
        alert("disableMute");
        vid.muted = false;
        vid.volume = 1.0;
    });

    //if i click this button first, the alert is "alert undefined"
    $("#checkMute").click(function(){
        alert('alert: ' + vid.muted); 
    });
}); 

1 个答案:

答案 0 :(得分:1)

正在选择视频并将其放入变量中。使用.muted不起作用,您需要使用jQuery的.prop()函数,正如我在this question中发现的那样。 这应该起作用:

HTML

<button id="enableMute" onclick="enableMute()" type="button">Mute</button>
<button id="disableMute" type="button">Enable</button>
<button id="checkMute" type="button">Status</button><br>

<video id="myVideo" width="320" height="176" autoplay loop muted>
    <source src="video/trailer.mp4" type="video/mp4">
    Your browser does not support HTML5 video.
</video>

JavaScript

$(function() {
  // Video is being selected
  var vid = $("#myVideo");

  $("#enableMute").click(function() {
    alert("enableMute");
    vid.prop('muted', true);
    vid.prop('volume', 1.0);
  });

  $("#disableMute").click(function() {
    alert("disableMute");
    vid.prop('muted', false);
    vid.prop('volume', 1.0);
  });

  $("#checkMute").click(function() {
    alert('alert: ' + vid.prop('muted'));
  });
});

要观看带有示例视频的实际操作,请转到this link