简体   繁体   English

无法使用 jQuery attr 静音 HTML5 视频

[英]Unable to Mute HTML5 video using jQuery attr

Trying to mute video using attr but it doesn't work.尝试使用attr使视频静音,但不起作用。

HTML HTML

 <video autoplay muted class='preview-video'>
       <source src='file.mp4' type='video/mp4'>
 </video>

jQuery jQuery

function volumeToggle(button) {
    var muted = $(".preview-video").attr("muted");
    $(".preview-video").attr("muted", !muted)
}

However, when I try prop() instead of attr() it works.但是,当我尝试prop()而不是attr()它起作用了。 Can someone explain in detail the reason behind it?有人可以详细解释背后的原因吗?

You need to toggle the muted property of the video element, not the attribute .您需要切换video元素的muted属性,而不是属性 Try this:尝试这个:

 let $vid = $('.preview-video'); $('button').on('click', function() { $vid.prop('muted', (i, m) => !m); });
 video { width: 300px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button>Toggle Mute</button> <video autoplay muted class='preview-video'> <source src="http://grochtdreis.de/fuer-jsfiddle/video/sintel_trailer-480.mp4" type="video/mp4"> </video>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM