视频静音/取消静音按钮 javaScript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33060291/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:04:31  来源:igfitidea点击:

Video mute/unmute button javaScript

javascriptif-statementaudiohtml5-videomute

提问by NAFarr

JavaScript beginner here!

JavaScript初学者在这里!

I am trying to make a video player in javaScript for a school project but I am having trouble with my mute button.

我正在尝试用 javaScript 为学校项目制作视频播放器,但我的静音按钮有问题。

I want the button to mute the video when clicked and unmute if the button is pressed again. So far I have only been able to mute the video and keep it muted.

我希望该按钮在单击时使视频静音,如果再次按下该按钮则取消静音。到目前为止,我只能将视频静音并保持静音。

Here is my current mute button.

这是我当前的静音按钮。

var button = document.getElementById('mute');
    button.onclick = function (){
        video.muted = true;
    };

I tried an if else statement but it was unsuccessful

我尝试了 if else 语句,但没有成功

var button = document.getElementById('mute');
    button.onclick = function (){

    if (video.muted = false) {    
           video.muted = true;
    }

    else {
        video.muted = false;
    }

    };

Thanks for your help.

谢谢你的帮助。

回答by David Li

if (video.muted = false) {    
       video.muted = true;
}

This should be

这应该是

if (video.muted === false) {    
       video.muted = true;
}

Or else the else statement will never run, since you are setting video.muted to false every time in the if statement itself.

否则 else 语句将永远不会运行,因为您每次在 if 语句本身中都将 video.muted 设置为 false。