Are IDs for an html element always available from the window object? [duplicate]

匿名 (未验证) 提交于 2019-12-03 01:06:02

问题:

This question already has an answer here:

I noticed the following:

...

Before realizing this, I was always using the following:

var x = document.getElementById('myDiv');  x.style.color = 'red'; 

I am confused. What's the point of the second approach? Does the first approach always work?

回答1:

Are IDs for an html element always available from the window object?

No. It is a non-standard Microsoft-ism that some other browsers have adopted for compatibility reasons. It is prone to namespace collisions, and not completely cross-browser compatible: don't do it.

What's the point of the second approach?

It is standard, well-supported cross-browser (and also cross-language).



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!