下面是一些示例代码。Javascript:访问没有getElementById的DOM元素
<body>
<div id="wrapper" class="access">
<form id="test">
<input id="password"></input>
<input type="submit"></input>
</form>
</div>
<script>
console.log(password);
alert(wrapper.className);
</script>
</body>
控制台日志返回id为password
的DOM元素。警报是“警报”。
它也以正常代码工作,例如, var x = wrapper.className;
这肯定是不对的? document.getElementById
,$('#password')
或goog.dom.getElement
如果只是这样工作,那么它有什么意义?现在这些功能只适用于遗产吗?
顺便说一下,它可以在Firefox,Chrome和Safari中运行。
一些浏览器定义ID为全局变量的元素。国际海事组织,使用'getElementById'。 –
你可以为此感谢IE。他们启动了它,其他人则为了兼容性而进行了追踪这被认为是不好的做法。坚持使用'document.getElementById'或者您为其提供示例的库中的等价物。 –
[JavaScript未定义变量可能是自动定义的?](http://stackoverflow.com/questions/21340666/javascript-undefined-variables-are-auto-defined),也可以:http://stackoverflow.com/问题/ 15767961/are-ids-for-html-element-always-available-from-the-window-object和http://stackoverflow.com/questions/3434278/ie-chrome-are-dom-tree-元素全局变量 - 在这里 –