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.

我想添加一个快速的小jQuery脚本来判断我的文档是否有重复的ID.它们可能是div、图像、复选框、按钮等的ID.

<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相关问答推荐

Flask,如何为ajax调用返回成功状态码

如果您的 Select 器对象无效,为什么 jQuery 不会炸弹?

在 jQuery 中,我想删除 div 中的所有 HTML

.slice(0) 在这里有什么意义?

如何制作 AngularJS 指令来停止传播?

Jquery .on('scroll')在滚动时不触发事件

使用 jQuery 将行添加到表的 tbody

JavaScript style.display="none" 还是 jQuery .hide() 更高效?

jQuery 对象和 DOM 元素

调用 e.preventDefault() 后提交表单

模态窗口中的 Twitter Bootstrap Datepicker

为不同的 node 类型配置jstree右键上下文菜单

将每 3 个 div 包装在一个 div 中

我如何知道 jQuery 是否有待处理的 Ajax 请求?

如何在 TypeScript 中获得 jQuery 自动完成功能?

在jQuery中,当它们都具有相同的名称时,如何获取单选按钮的值?

如何获取 jQuery 下拉值 onchange 事件

$(document).click() 在 iPhone 上无法正常工作. jQuery

textarea 的 val() 与 text()

jQuery中的wait()或sleep()函数?