检查DOM中的重复ID的JQuery [英] JQuery to check for duplicate ids in a DOM

查看:78
本文介绍了检查DOM中的重复ID的JQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET MVC编写应用程序。与传统的ASP.NET相比,您对生成的页面中的所有ID进行更多的负责。 ASP.NET将给你讨厌,但唯一的ids。



我想添加一个快速的小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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆