There are many ways to register events for some elements in html. Next we will introduce them in detail, you can test and compare the methods used according to your habits. There are multiple ways to register events for some elements in html.
First:
The Code is as follows:
Script
Function test ()
{
Alert ("OK ");
}
Script
Test
Second:
The Code is as follows:
Test
Script
Function test ()
{
Alert ("OK ");
}
Var x = document. getElementById ("");
X. onclick = test; // note that no ()
Script
Third (W3C model ):
The Code is as follows:
Test
Script
Function test ()
{
Alert ("OK ");
}
Var x = document. getElementById ("");
X. addEventListener ("click", test, false );
Script
Among the three parameters, addEventListener (), the first indicates the event type, note that there is no on, the second specifies the method, and the third indicates event bubbling (true) or event capture (false), which involves the sequence of events from bottom to top or from top to bottom.
RemoveEventListener () is used to remove an event. The parameter is the same as addEventListener.
In this mode, an error is reported in IE browser, which is executed normally in chrome.
Fourth (Microsoft model ):
The Code is as follows:
Test
Script
Function test ()
{
Alert ("OK ");
}
Var x = document. getElementById ("");
X. attachEvent ("onclick", test );
Script
Among the two parameters, attachEvent () indicates the first event type, with on and the second method.
The method for removing an event is detachEvent (). The parameter is the same as that of attachEvent.