为什么我的数组无法使用 innerhtml

Why is my array not working with innerhtml?

本文关键字:innerhtml 我的 数组 为什么      更新时间:2023-09-26

我正在制作一个网站,但我有一些代码不起作用。我将如何修复它?

在这里:

    <h1>Dmitry dances to</h1> <h1 id="danceto">Rock n' Roll</h1>
    <br>
    <input type="button" id="btnSearch" value="Search" onclick="danceTo();" />
    <script>
    var element = document.getElementById("danceto")
    var songs= new Array("Vocaloids","Dubstep","Cries of the Damned");
        var random = songs[Math.floor(Math.random() * songs.length)];
    function danceTo() {
    element.innerHTML = +songs+;
    }
    </script>

感谢您的任何帮助!

在你的代码中你有一些错误和错误的逻辑,我想你想要这个:

var element = document.getElementById("danceto")
var songs= new Array("Vocaloids","Dubstep","Cries of the Damned");
function danceTo() {
   var random = songs[Math.floor(Math.random() * songs.length)];
   element.innerHTML = random;
}
<h1>Dmitry dances to</h1> 
<h1 id="danceto">Rock n' Roll</h1>
<input type="button" id="btnSearch" value="Search" onclick="danceTo();" />