[英]Video mute/unmute button javaScript
JavaScript初学者在这里!
我正在尝试用 javaScript 为学校项目制作视频播放器,但我的静音按钮有问题。
我希望该按钮在单击时使视频静音,如果再次按下该按钮则取消静音。 到目前为止,我只能将视频静音并保持静音。
这是我当前的静音按钮。
var button = document.getElementById('mute');
button.onclick = function (){
video.muted = true;
};
我尝试了 if else 语句,但没有成功
var button = document.getElementById('mute');
button.onclick = function (){
if (video.muted = false) {
video.muted = true;
}
else {
video.muted = false;
}
};
谢谢你的帮助。
if (video.muted = false) {
video.muted = true;
}
这应该是
if (video.muted === false) {
video.muted = true;
}
否则 else 语句将永远不会运行,因为您每次都在 if 语句本身中将 video.muted 设置为 false。
video.muted ^= 1
原因是因为您实际上不是在检查 muted 属性是 false 还是 true,而是将 false 分配给 muted 属性。
所以你应该写如下:
if (video.muted === false) {
video.muted = true;
}
else {
// your code
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.