问题描述
我想在jQuery中设置一个静音/取消静音按钮.我已经在Stackoverflow上进行了一些搜索,到目前为止,这是我设法做到的:
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:
$("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?
推荐答案
在执行if( $("video").prop('muted', true) )
时,都将属性设置为true
,然后询问其是否正确.
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') )
可解决问题-这里是一个示例
Changing the condition to if( $("video").prop('muted') )
solves the problem - Here's an example.
还请注意,这将适用于页面上的所有视频,因此,如果您有多个播放器,则可能会造成混淆.
Also note this will work on all videos on a page so if you have more than one player it might get confusing.
这篇关于使用jQuery视频静音/取消静音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!