检查DOM中的重复ID的JQuery [英] JQuery to check for duplicate ids in a DOM
问题描述
我想添加一个快速的小jQuery脚本来检查我的文档是否有重复的ids。他们可能是DIVS,图像,复选框,按钮等的ID。
< div id =pnlMain>我的主面板< / div>
< div id =pnlMain>糟糕,我们不小心使用了相同的ID< / div>
我正在寻找一个设置并忘记的类型实用程序,当我做某些事情时会警告我是的,我只会在测试期间使用它,而且欢迎替代品(如firebug插件)。
以下内容将向控制台发出警告:
//警告重复的ID
$('[id]')。each(function(){
var ids = $('[id =' .id +']');
if(ids.length> 1&&ids [0] == this)
console.warn('Multiple IDs#'+ this.id);
});
I'm writing applications with ASP.NET MVC. In contrast to traditional ASP.NET you're a lot more responsible for creating all the ids in your generated page. ASP.NET would give you nasty, but unique ids.
I'd like to add a quick little jQuery script to check my document for duplicate ids. They may be ids for DIVS, images, checkboxes, buttons etc.
<div id="pnlMain"> My main panel </div>
<div id="pnlMain"> Oops we accidentally used the same ID </div>
I'm looking for a set and forget type utility that'll just warn me when I do something careless.
Yes i'd be using this only during testing, and alternatives (such as firebug plugins) are welcome too.
The following will log a warning to the console:
// Warning Duplicate IDs
$('[id]').each(function(){
var ids = $('[id="'+this.id+'"]');
if(ids.length>1 && ids[0]==this)
console.warn('Multiple IDs #'+this.id);
});
这篇关于检查DOM中的重复ID的JQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!