简体   繁体   English

当多个元素具有相同的id时如何输出错误?

[英]How to output an error when more than one element has the same id?

Because I don't have a strict naming convention for divs, spans, and inputs, I frequently give more than one input the same id by accident, which of course causes jQuery/javascript code to not work. 因为我对div,span和输入没有严格的命名约定,所以我经常不小心给多个输入相同的id,这当然会导致jQuery / javascript代码无法正常工作。

Giving more than one element the same id is not allowed, but I don't see any kind of error message anywhere when it happens, so when I do this it sometimes takes a lot of time before the "oh duh" moment where I realize what I did. 不允许为多个元素提供相同的ID,但是发生这种情况时,我在任何地方都看不到任何错误消息,因此,当我这样做时,有时会花很长时间才能意识到我做了什么。

Is there any way to know when more than one element has the same id? 有什么方法可以知道何时多个元素具有相同的ID?

This is going to be a standard practice you're going to learn through writing HTML over and over, time and time again! 这将是您要一次又一次地编写HTML来学习的标准实践! A good way to avoid using duplicate ID's would be to use less ID's and maybe use classes in their place. 避免使用重复ID的好方法是使用较少的ID,并可能在其位置使用类。

Only use ID's for important/structural elements on your website such as #header , #footer , #main , #slideshow , #content , #sidebar , etc. 仅使用ID对您网站的重要/结构元素,如#header#footer#main#slideshow#content#sidebar等。

If you're repeating ID's over and over on a page maybe you should be using classes. 如果您要在页面上一遍又一遍地重复ID,则可能应该使用类。 Any element/styled module that is going to be reused on that a multiple times should be identified using a class. 应该使用一个类来标识将要多次重复使用的任何元素/样式化模块。

Also, some IDE's will perform some special syntax highlighting/text-decoration to let you know that something funky is going on. 另外,某些IDE会执行一些特殊的语法高亮显示/文本修饰,以使您知道有些时髦。 Two that come to mind immediately are SublimeText2 and Visual Studio. 立即想到的两个是SublimeText2和Visual Studio。

Here's a jQuery snippet you can run to check for duplicate ID's on a page. 这是一个jQuery代码段,您可以运行该代码段来检查页面上是否有重复的ID。 It will let you know in your Console (F12 developer tools/Firebug) if there are any duplicates on the page. 如果页面上有任何重复项,它将在控制台(F12开发人员工具/ Firebug)中通知您。

(function () {  
    var found = false;  
    $('[id]').each(function () {  
        var ids = $('[id=' + this.id + ']');  
        if (ids.length > 1 && ids[0] === this) {  
            console.warn('ID used more than once: ' + this.id, ids);  
            found = true;  
        }  
    });  
    if (!found) {  
        console.log('No duplicate IDs found');  
    }  
})();  

You could throw this in your global footer so that it runs on every page, just be sure you take it out before going to production. 您可以将其放在全局页脚中,以使其在每个页面上运行,只需确保在生产之前将其取出即可。

Here's a JSFiddle: http://jsfiddle.net/A7h75/ 这是一个JSFiddle: http : //jsfiddle.net/A7h75/

Got the script from here . 从这里得到脚本

A good IDE will catch this for you. 一个好的IDE将为您抓住这一点。 I use Zend Studio (which is essentially a PHP specific version of Eclipse ) and it will catch this for you as you write your code. 我使用Zend Studio(本质上是Eclipse的PHP特定版本),在您编写代码时,它将为您效劳。 That's about as soon as you can catch it. 大约可以尽快抓住它。

You can check if your HTML is valid via the W3C Validator Service 您可以通过W3C验证程序服务检查您的HTML是否有效

This will show you all errors in your HTML and is advisable anyway just to check your HTML for errors that may cause cross-browser rendering problems 这将向您显示HTML中的所有错误,并且建议您仅检查HTML中是否存在可能导致跨浏览器呈现问题的错误,否则建议这样做。

Various extensions exist for Firefox and Chrome to check your HTML without having to visit the Validator service 'manually' Firefox和Chrome浏览器有各种扩展名,无需您手动访问Validator服务即可检查HTML。

you could utilize this function to check for duplicates 您可以利用此功能检查重复项

function getDuplicateIds(){
    var dups = [];
    $('[id]').each(function(){
        if($('[id="'+$(this).attr('id')+'"]').length > 1) dups.push($(this).attr('id'));
    })
    return $.unique(dups);
}

There's no way to turn on errors for this, but in a specific circumstance: 无法为此打开错误,但是在特定情况下:

$('[id={elementId}]').length > 1

should do the trick. 应该可以。

Edit: thanks for the correction. 编辑:感谢您的更正。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM