Javascript mute/unmute 视频使用图片

Javascript mute/unmute video using image

出于某种原因,它无法正常工作..我做错了什么? 我希望能够使用图像将视频静音和取消静音,这会改变视频是否静音。

 function mute() {
            var video = document.getElementById("bgvid");
         if (!video.muted) {
             document.getElementById('muteicon').src = "Images/muteicon.png";
            video.muted;
         }     else {
             document.getElementById('muteicon').src = "Images/soundicon.png";
             video.muted == false;
             }

        }
video#bgvid {
            margin-top: 30px;
            margin-bottom: 60px;

            width: 1280px; height: 720px;


            background-size: cover;

        }
        video { display: block; }
<video autoplay loop id="bgvid">
        <source src="newyork.mp4" type="video/mp4">
    </video>
    <br><br>
    <img alt="mute icon" src="Images/soundicon.png" id="muteicon" onclick="mute()">
    <script>
      </script>

您必须使用 assign(=) operator instead of the equality(==) 运算符:

function mute() {
    var video = document.getElementById("bgvid");

    if (!video.muted) {
        document.getElementById('muteicon').src = "Images/muteicon.png";
        // video.muted <- not assigned to true
        video.muted = true;
    } else {
        document.getElementById('muteicon').src = "Images/soundicon.png";
        // video.muted == false <- wrong use of equality operator
        video.muted = false;
    }
}

而且你不能告诉 API 将 属性 设置为 true 只是通过调用它来完成:

video.muted

您必须将 属性 分配给一个值:

video.muted = true

当您试图使视频静音时,您没有将 muted 属性 设置为 true

if (!video.muted) {
    document.getElementById('muteicon').src = "Images/muteicon.png";
    video.muted = true; // you need to assign this value to true
} else { ... }