为什么window [id] === document.getElementById(id) [英] why window[id] === document.getElementById( id )

查看:106
本文介绍了为什么window [id] === document.getElementById(id)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到在几个浏览器中,dom元素只能通过它们的ID访问:

I've noticed in several browsers, dom elements can be accessed simply by their id like this:

HTML

<div id="chocolat"></div>

JS

alert(chocolat.id); //alerts "chocolat
chocolat; //points to the node
window.chocolat; //idem
chocolat === document.getElementById('chocolat'); // true

(在此测试: http://jsfiddle.net/GUUPT/
这适用于Chrome上的某些版本的IE,但不适用于Firefox(5)。

( test here: http://jsfiddle.net/GUUPT/ ) This will work on some versions of IE, on Chrome, but it won't on Firefox(5).

我很好奇它来自哪里?是否符合标准?

I'm curious where does this come from ? is it in the standards?

推荐答案


  • 非标准

  • 它是由Internet Explorer(版本4 IIRC)启动的

  • IIRC它只能工作在Quirks模式下如果您使用的是最新版本的IE

  • 这篇关于为什么window [id] === document.getElementById(id)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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