我创建了一个表格,在它约800场。 不知不觉中我已经给同一ID在表单中仅有的两个领域。 如何追查呢?
Answer 1:
该http://validator.w3.org/将是方便的解决方案。 但是,使用jQuery你可以做这样的事情:
//See your console for duplicate ids
$('[id]').each(function(){
var id = $('[id="'+this.id+'"]');
if(id.length>1 && id[0]==this) {
console.log('Duplicate id '+this.id);
alert('duplicate found');
}
});
希望这可以帮助。
Answer 2:
这可以帮助你
来源:找到一个HTML页面上重复的ID
查找重复的ID的HTML页上
撰稿Eneko阿隆索在2011年5月6日
看起来有时我们忘记元素的ID都意味着是一个HTML页面上是独一无二的。 下面是我刚写找到一个页面上重复的ID(运行浏览器的JavaScript控制台上的代码)的代码一点点:
var idList = {};
var nodes = document.getElementsByClassName('');
for (var i in nodes) {
if (!isNaN(i) && nodes[i].id) {
idList[nodes[i].id] = idList[nodes[i].id]? idList[nodes[i].id]+1:1;
}
}
for (var id in idList) {
if (idList[id] > 1) console.log("Duplicate id: #" + id);
}
Answer 3:
我创建了一个例子给你看看,发现所有的表单/元素中重复的ID的网页上,并打印重复的ID名称到控制台。
该阵列contains
方法取自此信息 。
<html>
<body>
<form id="frm">
<input type="text" id="a" />
<input type="text" id="b" />
<input type="text" id="c" />
<input type="text" id="d" />
<input type="text" id="e" />
<input type="text" id="f" />
<input type="text" id="a" />
<input type="text" id="h" />
<input type="text" id="i" />
<input type="text" id="j" />
<input type="text" id="d" />
<input type="text" id="l" />
</form>
</body>
<script type="text/javascript">
Array.prototype.contains = function(obj) { //Add a 'contains' method to arrays
var i = this.length;
while (i--) {
if (this[i] === obj) {
return true;
}
}
return false;
}
frm = document.getElementById('frm'); //Get the form
els = frm.getElementsByTagName('input'); //Get all inputs within the form
ids = new Array(els.length); //Create an array to hold the IDs
for(e = 0; e < els.length; e++) { //Loop through all of the elements
if(ids.contains(els[e].id)) //If teh array already contains the ID we are on
console.log('Duplicate: '+els[e].id); //Print 'Duplicate: {ID}' to the console
ids.push(els[e].id); //Add the ID to the array
}
</script>
</html>
上面的代码将输出以下内容:
复制:一
复制:d
Answer 4:
只使用阵列的方法之一十岁上下内胆:
[].map.call(document.querySelectorAll("[id]"),
function (e) {
return e.id;
}).filter(function(e,i,a) {
return ((a.lastIndexOf(e) !== i) && !console.log(e));
})
原木每重复,并且如果任何被发现返回包含ID的数组。
Answer 5:
有一个名为DUP-ID的浏览器扩展,如果您安装,你只需要按下按钮,检查重复的ID。 安装的链接: https://chrome.google.com/webstore/detail/dup-id-scans-html-for-dup/nggpgolddgjmkjioagggmnmddbgedice
Answer 6:
通过defualt 记事本++有语法高亮,如果你双击一个词来选择它,它会突出同一个词的所有其他出现次数。 你将不得不做的体力劳动重命名一些东西(可能很多),我的意思是,因为字段不能拿出自己独特的名字。
文章来源: How to find duplicate id's in a form?