Is there a spec that the id of elements should be made global variable?

前端 未结 5 2113
死守一世寂寞
死守一世寂寞 2020-11-22 05:34

If I have a

in Chrome then in javascript I can do a.stuff() (it\'s like as if a is a global variable).
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 05:41

    Technically, this question is opinion, but it's a good question.

    IE does this as well and it has caused headaches for some.

    The rules for naming variables in JavaScript and IDs in HTML are different. I can't see how this is a good thing.

    For instance, on this page there is an element with an ID of "notify-container". That's not a valid JavaScript name at all.

    Also, when are these names bound? If an inline script declares a variable and then the element appears later, which has precedence?

    It's cannot be made consistent.

提交回复
热议问题