为什么我的数组不能使用innerhtml?

时间:2016-03-15 22:03:28

标签: javascript html

我正在制作一个网站,而且我有一些代码无效。我该怎么办呢?

这是:

    <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>

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

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

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();" />