Sometimes we need to check whether a page with a duplicate ID, the general ID is unique, but also convenient control, then you can refer to the following code
Method One:
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<TITLE> New Document </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 Time: ' + (new Date ()-time);
return;
}else{
Ret[id] = true;
}
}
}
alert ("No same ID found");
}
</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>");
};
//insertion of a random ID in a random position
i = parseint (Math.random () * 1000);
Html[i] = Html[i] + ("<div id= ' a" + rnd + "' >" + rnd + "</div>");
document.write (Html.join (""));
})();
</script>
</BODY>
</HTML>
Method Two:
Copy Code code as follows:
<! 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 < 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 to result) {
Arr.push (Result[o]);
}
Alert (Arr.join ("T"));
};
</script>
</HEAD>
<BODY>
</BODY>
</HTML>