Add an event as an object in JS

For example:
In js, in order to add a button onclick event would write:

button.onclick  =  ClickEvent;

ClickEvent is a function.
In the absence of such ClickEvent parameter it is possible, but at the time ClickEvent need a parameter to die.

Then you can add like this:

button.attachEvent( " onclick " function (){ClickEvent(event,  ' params ' )});

Of course, this can also be added in the absence of the parameter, this is the best method.

There is another way:

div.addEventListener( " click  " ,ClickEvent);
Here, we recommended the second approach, namely attachEvent method. However, the second method in FireFox, Chrome middle of nowhere, you can use the third method.
In addition, setAttribute method does not work, no argument is not acceptable. [It is used to set the property of the object]

Reproduced in: https: //www.cnblogs.com/zhangchenliang/archive/2011/06/07/2074475.html

Guess you like

Origin blog.csdn.net/weixin_33860553/article/details/93496133