简体   繁体   English

为什么我的JS函数不能与按钮一起使用?

[英]Why doesn't my JS function work with my button?

HTML HTML

                <audio id="song" controls> <source src="song.mp3"></source>
                </audio>
                <button class="controls" id="play">Play</button>

JavaScript JavaScript的

            var play = document.getElementById("song");
            song.addEventListener ("click", buttonActions);
            function buttonActions(event){
                song.play();
            }

You're attaching the event handler to the <audio> element, instead of the <button> element. 您将事件处理程序附加到<audio>元素,而不是<button>元素。

Instead of: 代替:

var play = document.getElementById("song");
song.addEventListener ("click", buttonActions);    
function buttonActions(event){
    song.play();
}

It should be: 它应该是:

var play = document.getElementById("play");
play.addEventListener ("click", buttonActions);    
function buttonActions(event){
    var song = document.getElementById("song");
    song.play();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM