javascript - Accessing elements with IDs via their global variables -
it not-so-widely-known fact most* web browsers create global variable every element on page id
attribute:
html:
<header id="page-header"></header>
js:
window['page-header'].style.fontfamily = "comic sans ms";
questions:
- is reliable way select elements?
- any reason use
document.getelementbyid
instead? guess accessing id'd elements global variables fasterdocument.getelementbyid
.
here demo.
*i've tested in latest versions of chrome, firefox, , ie.
i tested on jsperf , chromium v25 getelementbyid faster
Comments
Post a Comment