未定义“音频”,未定义“文档”

时间:2018-11-25 14:39:39

标签: javascript html

试图向我的网页添加一些音乐(仅静音/取消静音按钮),但我不知道该怎么做

HTML代码:

<script type="text/javascript" src="Mymusic.js"></script>
    <div class="audiodiv">
    <img src="img/Speaker.png" id="mutebtn">
    </div>

JavaScript文档:

var audio, mutebtn;
function initAudioPlayer(){
    audio = new Audio();
    audio.src = "audio/MyAudio.mp3";
    audio.loop = true;
    audio.play();
    mutebtn = document.getElementById("mutebtn");
    mutebtn.addEventListener("click", mute);
    function mute(){
        if(audio.muted){
            audio.muted = false;
            document.getElementById("mutebtn").src = "img/Speaker.png"
        } else {
            audio.muted = true;
            document.getElementById("mutebtn").src = "img/Muted.png"
        }
    }
}
window.addEventListener("load", initAudioPlayer);

我收到两条错误消息,提示未定义“音频”和未定义“文档”。怎么了?

0 个答案:

没有答案