html element id as javascript variable

前端 未结 3 1998
后悔当初
后悔当初 2021-02-19 16:43

Consider the following code:





                      
相关标签:
3条回答
  • 2021-02-19 17:18

    Yes, it's been around for a very long time, which is likely the only reason it exists. Removing such a "feature" will break compatibility with existing web sites, which browser vendors are very reluctant to do.

    You're correct in saying that it's not a good idea. It's a profoundly bad idea and can lead to naming conflicts as well as unnecessary pollution of the global namespace.

    0 讨论(0)
  • 2021-02-19 17:21

    First off, how long has this "feature" been around?

    It is a Microsoft-ism that cropped up around IE 4 if I remember correctly.

    Some other browsers have added support for it in an effort to be compatible with badly written code that depends on it. Some may only support it in quirks mode.

    this doesn't seem like a very good "feature" to have

    Correct. Don't use it. :)

    0 讨论(0)
  • 2021-02-19 17:31

    It's an old IE feature, not recommended to use. Several browsers implement it, often as nonenumerable properties of the window object. FF, for example, does it only in quirks mode.

    For further reading see duplicate Is there a spec that the id of elements should be made global variable?...

    0 讨论(0)
提交回复
热议问题