js检查页面上有无重复id

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
 
<TITLE> New Document </TITLE>
 
<script type="text/javascript">
   
function createDiv(){
       
var html = [], id;
       
for(var i = 0; i < 2000; i++){
            id
= "divId" + parseInt(Math.random() * 10000);
            html.push(
"<div id='" + id + "'>" + id + "</div>");
        }
        document.body.innerHTML
= html.join("");
    }
    window.onload
= function(){
        createDiv();
       
       
var oID = {} , result = {}, arr = [],
            tags
= document.getElementsByTagName("*");
       
for(var i = 0, id; i < tags.length; i++){
            id
= tags[i].id;
           
if(id){
                oID[id]
= oID[id] ? oID[id] + 1 : 1;
               
if(oID[id] > 1){
                    result[id]
= id + " " + oID[id]; } } } for(var o in result){
            arr.push(result[o]);
        }
        alert(arr.join(
"/t"));
    };
 
</script>
</HEAD>

<BODY>
</BODY>
</HTML>

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页