• js检查页面上有无重复id的代码分享


    用js代码检查一个页面上是否用重复的id。

    方法一:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE>检查有无重复的ID-www.jbxue.com</TITLE>
    <script type="text/javascript">
    window.onload = function(){
    var tags = document.getElementsByTagName("*"),
    count = tags.length, time, ret = {}, id;
    time = new Date();
    for(var i = 0; i < count; i++){
    id = tags[i].id;
    if(id){
    if(ret[id]){
    alert(id + "/n用时:" + (new Date() - time));
    return;
    }else{
    ret[id] = true;
    }
    }
    }
    alert("未找到相同ID");
    }
    </script>
    </HEAD>
    <BODY>
    <script type="text/javascript">
    (function(){
    var html = [], rnd = parseInt(Math.random() * 1000);
    for(var i = 0; i < 1000; i++){
    html.push("<div id='a" + i + "'>" + i + "</div>");
    };
    //在随机位置插入一个随机的id
    i = parseInt(Math.random() * 1000);
    html[i] = html[i] + ("<div id='a" + rnd + "'>" + rnd + "</div>");
    document.write(html.join(""));
    })();
    </script>
    </BODY>
    </HTML>

    方法二:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE>检查有无重复id-www.jbxue.com</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>
  • 相关阅读:
    MJRefreshFooterView
    UIActionSheet
    UIAlertView带textField
    SIAlertView
    旋转 锚点
    centos7.2 Apache+PHP7.2+Mysql5.6环境搭建
    ubuntu16.04 mysql 开启远程连接
    Ubuntu16.04重新安装MySQL数据库
    Ubuntu16.04彻底卸载MySQL
    laravel框架基础(2)---laravel项目加载机制
  • 原文地址:https://www.cnblogs.com/cfinder010/p/3197193.html
Copyright © 2020-2023  润新知