关于 JavaScript 中数组的查询://颜色仅在引用时才有效,原因?我得到一个这样的数组,需要将随机值从以下颜色数组传递到我的 URL,但它不起作用。
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to display a random number.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
//**num** works both ways, even when they are quoted or if I use the commented line.
var num = new Array('1','2','3','4','5','6','7','6');
//var num = new Array(1,2,3,4,5,6,7,6);
//**colors** only works when quoted, reason? I am getting an Array like this and need to pass the random values from following colors array to my URL, but it doesn't work.
var colors= new Array('red','blue','green','orange','cyan','yellow', 'black');
//var colors= new Array(red,blue,green,orange,cyan,yellow, black);
var item = num [Math.floor(Math.random()*num .length)];
var item2= colors[Math.floor(Math.random()*colors.length)];
document.getElementById("demo").innerHTML=item +" : "+ item2;
}
</script>
</body>
</html>