Why JS function name conflicts with element ID?

后端 未结 3 1088
失恋的感觉
失恋的感觉 2020-11-22 06:19

I\'ve got two almost identical simple JS fiddles calling a function on select change. Function name is the same as select ID in both cases, but for some reason the first fid

3条回答
  •  我寻月下人不归
    2020-11-22 06:50

    IE automatically reserves a var ID = domElement; in the global space for each DOM-Element with an ID. Some other browsers adopted this behaviour.

    Always try to avoid using same IDs and varnames! Alternatively, use your own namespace in JS to avoid the collisions.

    EDIT:

    I don't know why one of your examples fails, while the other one works. It might be a simple timing/order of execution -issue caused by the wrapping

    .

提交回复
热议问题