问题
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