From:http://blog.csdn.net/china_skag/article/details/6915323
判断网页中的ID是否有重复的:指定ID判断
$(function(){
$("body [id]").each(function(){
var ids = $(this).attr("id");
if( $("body [id="+ids+"]").length >= 2 ){
alert("id为"+ids+" 的重复了。");
}
});
})
判断网页中的ID是否有重复的:不指定ID判断
function chkid() {
var did = {}
divs = document.getElementsByTagName("*");
for (var i = 0, l = divs.length; i < l; i++) {
divs[i].getAttribute("id")
? did[divs[i].getAttribute("id")] === undefined
? did[divs[i].getAttribute("id")] = 0
: did[divs[i].getAttribute("id")]++
: 0;
}
for (var i in did) {
did[i]
? alert("id " + i + "重复了" + (did[i] + 1) + "次")
: 0;
}
}