无法使随机div可见

时间:2015-08-21 16:32:04

标签: javascript

我有一个名为compliments的数组,其中包含我的ID,我尝试将随机ID显示为可见但不起作用

var compliments=new Array()

compliments[0]="divid"

compliments[1]="divid2"

compliments[2]="divid"

compliments[3]="divid2"

compliments[4]="divid"

compliments[5]="divid2"

compliments[6]="divid" 
document.getElementById(compliments[Math.round(Math.random()*compliments.length-1)).style.visibility ="visible";

2 个答案:

答案 0 :(得分:1)

您的代码中没有关闭方括号。

compliments[Math.round(Math.random()*compliments.length-1)]



var compliments=new Array()

compliments[0]="divid"

compliments[1]="divid2"

compliments[2]="divid"

compliments[3]="divid2"

compliments[4]="divid"

compliments[5]="divid2"

compliments[6]="divid" 


document.getElementById(compliments[Math.round(Math.random()*compliments.length-1)]).style.visibility ="visible";

<div id="divid" style="visibility: hidden;">Test1</div>
<div id="divid2" style="visibility: hidden;">Test2</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你走了:

var compliments = ["divid", "divid2", "divid", "divid2", "divid", "divid2", "divid"];
var random = Math.floor(Math.random() * compliments.length);
var div = document.getElementById(compliments[random]);
div.style.visibility = "visible";