使用 jQuery 进行视频静音/取消静音
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21852284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Video mute/unmute with jQuery
提问by oneday
I'd like to make a mute/unmute button in jQuery. I've done some searching on Stackoverflow and this is what I managed to do so far:
我想在 jQuery 中制作一个静音/取消静音按钮。我已经在 Stackoverflow 上做了一些搜索,这就是我到目前为止所做的:
$("video").prop('muted', true);
$("#mute-video").click( function (){
if( $("video").prop('muted', true) )
{
$("video").prop('muted', false);
}
else {
$("video").prop('muted', true);
}
});
but for some reason it's only able to unmute, not to mute back.
但出于某种原因,它只能取消静音,不能静音。
Any idea what's wrong with the code?
知道代码有什么问题吗?
回答by Ronny
When you're doing if( $("video").prop('muted', true) )
you're both setting the property to true
and then ask if it's true.
当您这样做时,if( $("video").prop('muted', true) )
您都将属性设置为true
,然后询问它是否为真。
Changing the condition to if( $("video").prop('muted') )
solves the problem - Here'san example.
更改条件以if( $("video").prop('muted') )
解决问题 -这是一个示例。
Also note this will work on all videos on a page so if you have more than one player it might get confusing.
另请注意,这适用于页面上的所有视频,因此如果您有多个播放器,则可能会造成混淆。