方法1: 复制代代码如下: 新しいドキュメント <br> window.onload = function(){<br> var tags = document.getElementsByTagName("*"),<br> count = tags.length, time, ret = {}, id;<br> time = new Date();<br> for(var i = 0; i id = tags [i].id;<br> if(id){<br> if(ret[id]){<br> alert(id "/n用時間:" (new Date() - time));<br> return;<br> }else{<br> ret[id] = true;<br> }<br> }<br> alert("未找同ID");<br> }<br> <br> (function(){<br> var html = [ ], rnd = parseInt(Math.random() * 1000);<br> for(var i = 0; i html.push("<div id='a" i "'>" i "</div>");<br> };<br> // 随机位置插入一个随机的id<br> i = parseInt(Math.random() * 1000);<br> html[i] = html[i] ("<div id='a" rnd "'>" rnd "</div>");<br> document.write(html.join("" ));<br> })();<br> 方法2: 复制代码代码如下: New Document <br> function createDiv(){<br> var html = [], id;<br> for(var i = 0; i < 2000; i ){<BR> id = "divId" parseInt(Math.random() * 10000);<BR> html.push("<div id='" id "'>" id "</div>");<br> }<br> document.body.innerHTML = html.join("");<br> }<br> window.onload = function(){<br> createDiv();<br><br> var oID = {} , result = {}, arr = [],<br> tags = document.getElementsByTagName("*");<br> for(var i = 0, id; i < tags.length; i ){<BR> id = tags[i].id;<BR> if(id){<BR> oID[id] = oID[id] ? oID[id] 1 : 1;<BR> if(oID[id] > 1){<br> result[id] = id " " oID[id]; } } } for(var o in result){<br> arr.push(result[o]);<br> }<br> alert(arr.join("/t"));<br> };<br>