How can I pass arguments to anonymous functions in JavaScript?

前端 未结 10 1462
别那么骄傲
别那么骄傲 2020-12-08 01:37

I\'m trying to figure out how to pass arguments to an anonymous function in JavaScript.

Check out this sample code and I think you will see what I mean:



        
10条回答
  •  被撕碎了的回忆
    2020-12-08 02:12

    Your specific case can simply be corrected to be working:

    
    

    This example will work because the anonymous function created and assigned as a handler to element will have access to variables defined in the context where it was created.

    For the record, a handler (that you assign through setting onxxx property) expects single argument to take that is event object being passed by the DOM, and you cannot force passing other argument in there

提交回复
热议问题