<div id=zcy_yingcang>1111111111</a></div>
<br>
<div id=zcy_yingcang>2222222222</a></div>
<a style="cursor:hand" onclick="javascript:if (document.getElementById('zcy_yingcang')) document.getElementById('zcy_yingcang').style.display='none';">隐藏层</a>
页面有相同的id名称,则出错,请问怎么做
<br>
<div id=zcy_yingcang>2222222222</a></div>
<a style="cursor:hand" onclick="javascript:if (document.getElementById('zcy_yingcang')) document.getElementById('zcy_yingcang').style.display='none';">隐藏层</a>
页面有相同的id名称,则出错,请问怎么做
既然是ID,就应该不一样才对。
非要搞定这个问题的话,用getElementsByTagName()取得所有同标签名的元素,然后遍历比较各自的ID值。
以前页面处理广告错了,我想在尾部进行一个js调用处理页面,id不能改,只能是相同的id
Array.prototype.exists = function(value) {
return ("," + this.join(",") + ",").indexOf("," + value + ",") > -1
}
function test(){
var all = document.getElementsByTagName("div");
var array = [];
for (var i = 0, l = all.length - 1; i < l; i++) {
if (array.exists(all[i].id)) {
alert(all[i].id)
return;
}
else {
array.push(all[i].id);
}
}
}
var arr = document.getElementsByTagName("div");
for(i=0;i<arr.length;i++){if(arr[i].id=="zcy_yingcang") arr[i].style.display = "none";