There are many ways to register events for some elements in html.
First:
Copy codeThe Code is as follows:
<Script>
Function test ()
{
Alert ("OK ");
}
</Script>
<A href = "#" onclick = "test ()"> test </a>
Second:
Copy codeThe Code is as follows:
<A href = "#" id = "a"> test </a>
<Script>
Function test ()
{
Alert ("OK ");
}
Var x = document. getElementById ("");
X. onclick = test; // note that no ()
</Script>
Third (W3C model ):
Copy codeThe Code is as follows:
<A href = "#" id = "a"> test </a>
<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 ):
Copy codeThe Code is as follows:
<A href = "#" id = "a"> test </a>
<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.