What are the drawbacks of accessing DOM elements directly by ID?

后端 未结 3 1768
孤街浪徒
孤街浪徒 2020-12-19 19:54

Today I stumbled upon the possibility to access a DOM element in Javascript simply by its id e.g. like this:

elementid.style.backgroundColor = \"blue\"
         


        
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-19 20:36

    It’s not a part of any standard. Besides, simple variables can be overwriten. Var content could be redeclared in any place of your script, or external library (and, yes, I know global variables are evil, but ppl still use them…) and your script will break.

    And, naturally, you cannot use IDs like alert or document etc.

提交回复
热议问题