Javascript Function and Form Name conflict

前端 未结 2 442
清歌不尽
清歌不尽 2020-12-01 19:48

I have a javascript function that would not run and threw an error. It took about an hour to realize that my form had the same name as the function. It seemed odd that a for

2条回答
  •  無奈伤痛
    2020-12-01 20:24

    The function is a member in the window object, and the form is a member of the forms collection in the document object. When the script looks for a match for the identifier "mytest", it first looks in the document object, and if it's not found there it looks in the window object.

    You can access the function even with the conflicting form name if you specify that it's in the window object:

    Click Me
    

提交回复
热议问题