使用非唯一ID的未预测行为 [英] Unpredicted behavior of using non-unique IDs

查看:80
本文介绍了使用非唯一ID的未预测行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当人们说对不同的DOM元素使用相同的ID是错误的,我完全同意。我的意思是,没有什么可以讨论的。

When people are saying that using identical id's for different DOM elements is wrong, I totally agree. I mean, there is no even something to discuss.

但是实际上意思是短语如果你将在页面上有几个同名的id,保证工作?

But what is actually meant by the phrase "if you will have several eponymous ids on the page, that doesn't guaranteed to work"?

实际上什么不能保证工作?我们可以将ID作为常规属性,因此,我们可以找到 document.querySelectorAll('[id =container]')的所有div。通过getAttribute / setAttribute访问也应该工作。如果我们试图通过 document.getElementById 获取一个元素 - 好吧,我没有测试,但最可能的选择器引擎将返回第一个元素遍历树。我可以想象这不能保证。还有什么?

What actually does not guaranteed to work? We can treat ID as a regular attribute, thus, we can find all divs that document.querySelectorAll('[id="container"]'). Accessing through getAttribute/setAttribute should work as well. If we are trying to fetch an element via document.getElementById - well, I haven't tested, but most probably selector engine will return first element it find traversing the tree. I can imagine this can not be guaranteed. What else?

CSS将会很好工作 - #container {background-color:red} 与红色所有找到的容器(或我错了)。有人可以告诉我在标准规格这样的行为不能保证在哪里?

CSS will work pretty fine - #container {background-color: "red"} will paint with red all found containers (or am I wrong). Can somebody show me where in standard spec such behaviour is not guaranteed?

那么,实际上不能保证什么呢?再次,我确认使用相同的ID是错误的事实。我也不想讨论应用id-heavy css规则的性能问题。问题是与C / C ++程序员称之为不可预测的行为非常相似的东西。

So, what is actually not guaranteed? Once again, I do acknowledge the fact that using same IDs is wrong. Neither do I want discuss perfomance issues of applying id-heavy css rules. The question is about something which is quite similar to what C/C++ programmers call "unpredicted behavior".

UPD:甚至愚蠢为你,那么很好,只是提供了一个不可预测的ID相关行为在特定浏览器的例子。至少对于我来说,这是比好,任何不能保证,你怎么不能得到它。更有用的答案。

UPD: If it sounds hard or even stupid for you, then well, just provide an examples of unpredictable ID related behavior in specific browser. At least, as for me, it is more useful answer than "well, anything is not guaranteed, how can not you get it!".

推荐答案

规范说ID应该是唯一的。

The spec says that ID's are supposed to be unique.

这意味着浏览器本身没有义务实现代码,可以支持多个非unquie ID。它可以假设当构建ids是唯一的并且可用作键的DOM模型时。他们可能在某些浏览器,今天工作。但也许明天他们不会。只是因为他们发生在您测试的浏览器中运作,并不意味着他们将在未来的版本,或他们表现为不同的不可重复的方式。

That means that the browser itself has no obligation to implement code that can support multiple non-unquie IDs. It can assume when it builds the DOM model that ids are unique and are usable as keys. They may work in some browsers, today. But maybe tomorrow they won't. Just because they happen to function in the browsers you have tested, doesn't mean they will in future versions, or they behave as such in a different non-repeatable manner.

也许第一次在一个星期一加载一个页面document.getElementById可能会返回第一个元素,如果它是在星期二下午5点后,它在一个SSL网站,那么也许document.getElementById引发异常。也许一个新版本的firefox不会应用css属性过去的第一次出现的id。谁知道。浏览器开发人员不必关心 ,这是不保证工作的意思。

Maybe the first time you load a page on a monday document.getElementById might return the first element, if it's after 5pm on a tuesday and it's on an SSL site, then maybe document.getElementById throws an exception. Maybe a new release of firefox doesn't apply css attributes to anything past the first occurance of the id. Who knows. The browser developers don't have to care, that is what "not guaranteed to work" means.

这篇关于使用非唯一ID的未预测行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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