此函数应该用逗号输出随机字符,但没有输出
<p id="demo">Click the button to display a random number.</p>
<button onclick="myFunction()">Submit</button>
<script>
function myFunction()
{
var random_list = randomizeList([1,2,3,4,49,'foo',38,'bar',9,-1]);
var n=random_list.split(" ");
document.getElementById("demo").innerHTML=n;
}
</script>
答案 0 :(得分:1)
如果您打算使用split
功能,则列表应为CSV字符串。拆分后使用随机数来获取数组的索引。
<强> HTML 强>
<p id="demo">Click the button to display a random number.</p>
<button id="mybutton">Submit</button>
<强>的Javascript 强>
var button = document.getElementById("mybutton");
button.onclick = function(){myFunction()}
function myFunction()
{
var random_list = "1,2,3,4,59,foo,38,bar,9,-1";
var n=random_list.split(",");
document.getElementById("demo").innerHTML=n[Math.floor((Math.random()*n.length))];
}