问题描述
例如我有一个 id的元素=mybox
属性,用 document.getElementById('mybox')
和 mybox调用它是否有任何区别
直接,因为我看到它们在大多数浏览器中都一样吗? jsfiddle显示实例
For example I have an element with id="mybox"
attribute, is there any difference between calling it with document.getElementById('mybox')
and mybox
directly, as I see both work same in most browsers? The jsfiddle shows live example http://jsfiddle.net/usmanhalalit/TmS3k/
如果没有区别那么为什么 document.getElementById('mybox')
如此受欢迎,直接调用 mybox
是不好的做法?
If there is no difference then why document.getElementById('mybox')
is so popular, is it a bad practice to call mybox
directly?
推荐答案
某些呈现模式中的一些浏览器将为每个具有id的元素创建一个全局变量。
Some browsers in some rendering modes will create a global variable for each element with an id.
它是非标准的,无处不在,绝对不能依赖。
It is non-standard, won't work everywhere and definitely can't be depended upon.
这篇关于document.getElementById('mybox')和mybox有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!