document.getElementById(“someId”) 与某某
这个问题可能看起来很愚蠢,但是使用 document.getElementById("someId") 访问元素(id为“someId”)与使用 document.getElementById("someId") 访问元素(id为“someId”)有什么区别?只是输入 someId 吗?
例如:
document.getElementById("someId").style.top = "12px";
vs
someId.style.top = "12px";
这是一个示例代码 http://jsfiddle.net/pRaTA/ (我发现它没有不能在 Firefox 中工作)
This question might seem silly, but what's the difference between accessing an element (with id "someId") using document.getElementById("someId") Vs. just typing someId ?
eg:
document.getElementById("someId").style.top = "12px";
vs
someId.style.top = "12px";
Here's a sample code http://jsfiddle.net/pRaTA/ (I found that it doesn't work in firefox)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不同之处在于,虽然
someId
在某些浏览器中工作,但document.getElementById("someId")
实际上符合 W3C 标准。The difference is that while
someId
works in some browsers,document.getElementById("someId")
actually complies with the W3C standard.声明元素 DOM id 并不意味着它可以在所有浏览器中作为全局变量使用。实现这一点的唯一交叉兼容方法是首先执行。
编辑:我编写了这个测试代码,它验证基于 webkit 的浏览器似乎可以将 id 作为 var 提供,而无需先声明它。根据 this,IE 也会显示此行为。
代码:
Declaring a element DOM id doesn't mean it's available as a global variable in all browsers. The only cross compatible way to get that is to first do.
Edit: I made this test code which verifies that webkit based browsers seem to make the id available as a var without first declaring it. According to this, also IE will show this behaviour.
Code:
在客户端脚本中,我们执行 document.getElementById 来获取网页中的元素 obj。从 DOM 中检索特定元素及其属性和属性;方法,您需要使用 getElementById 方法。在 Jquery 中,你可以简单地执行 var objX = $('#id') ,
因为直接写下 id 是不行的。因为如何在不遍历文档的情况下从 DOM 中提取元素 obj 。 document.getElementById 方法将从文档中提取元素信息。我希望这有一定道理。
In client side scripting, we do document.getElementById to get the element obj in the web page. To retrieve the particular element from the DOM and its properties & methods, you need to use the getElementById method. In Jquery you can simply do var objX = $('#id')
Where as writing down id straight will not do the job. Because how will you extract the element obj from the DOM without traversing the document. document.getElementById method will pull the element information from the document. I hope this make some sense.
仅使用
someId
是选择元素的一种旧方法(我认为这是由 IE 实现的)。 document.getElementById 是适用于所有浏览器的标准方法。考虑到您只支持旧版IE浏览器的场景。 document.getElementById 仍然更可靠和可读。如果您使用所有数字作为 id,则可靠。
例如:
Using just the
someId
is an old way of selecting an element (i think this was implemented by IE). document.getElementById is the standard one which works on all browsers.Considering the scenario that you only support the old IE browsers. document.getElementById is still more reliable and readable. Reliable if you are using all numbers for ids.
for example:
将元素作为以元素 ID 命名的全局对象的属性来访问(您的第二个示例)是一种起源于 IE 的便利方法,已被 Safari 和 Chrome 复制。这是一个非常糟糕的主意,你不应该使用它。主要问题是命名冲突(例如,具有完全有效 ID 的元素,例如“alert”或“document”)。
另一方面,
document.getElementById()
是长期建立的 DOM 标准,除了 一些愚蠢的 IE 缺陷,是万无一失的。始终优先使用它而不是其他形式。Accessing elements as properties of the global object named after the ID of the element (your second example) is a convenience that originated in IE and has been copied by Safari and Chrome. It's a really bad idea and you shouldn't use it. The main issue is naming collisions (for example, elements with perfectly valid ids such as "alert" or "document").
document.getElementById()
, on the other hand, is long-established DOM standard and, apart from some idiotic IE flaws, is foolproof. Always use it in preference to the other form.