JQuery 检查 DOM 中的重复 ID [英] JQuery to check for duplicate ids in a DOM
问题描述
我正在使用 ASP.NET MVC 编写应用程序.与传统的 ASP.NET 相比,在生成的页面中创建所有 id 的责任要大得多.ASP.NET 会给您带来讨厌但唯一的 ID.
我想添加一个快速的小 jQuery 脚本来检查我的文档是否有重复的 ID.它们可能是 DIVS、图像、复选框、按钮等的 ID.
我的主面板</div><div id="pnlMain">糟糕,我们不小心使用了相同的 ID </div>我正在寻找一个设置并忘记类型的实用程序,它会在我粗心大意时警告我.
是的,我只会在测试期间使用它,也欢迎使用替代品(例如 firebug 插件).
解决方案 以下内容将在控制台中记录警告:
//警告重复的 ID$('[id]').each(function(){var ids = $('[id="'+this.id+'"]');if(ids.length>1 && ids[0]==this)console.warn('多个 ID #'+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);
});
这篇关于JQuery 检查 DOM 中的重复 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文