Video mute/unmute with jQuery

故事扮演 提交于 2019-11-27 17:22:47

问题


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?


回答1:


When you're doing if( $("video").prop('muted', true) ) you're both setting the property to true and then ask if it's true.

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.



来源:https://stackoverflow.com/questions/21852284/video-mute-unmute-with-jquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!