我正在使用 ASP.NET MVC 编写应用程序。与传统的 ASP.NET 相比,您需要更多地负责在生成的页面中创建所有 ID。 ASP.NET 会给你讨厌但唯一的 ID。
我想添加一个快速的小 jQuery 脚本来检查我的文档是否有重复的 ID。它们可能是 DIVS、图像、复选框、按钮等的 ID。
<div id="pnlMain"> My main panel </div>
<div id="pnlMain"> Oops we accidentally used the same ID </div>
我正在寻找一个一劳永逸的实用程序,它会在我粗心大意时发出警告。
是的,我只会在测试期间使用它,也欢迎使用替代品(例如 Firebug 插件)。
最佳答案
以下将向控制台记录警告:
// 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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/482763/