Video mute/unmute button javaScript

余生长醉 提交于 2019-12-12 00:22:11

问题


JavaScript beginner here!

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

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

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

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

    else {
        video.muted = false;
    }

    };

Thanks for your help.


回答1:


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.



来源:https://stackoverflow.com/questions/33060291/video-mute-unmute-button-javascript

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