The main purpose of attachEvent () is to add an event to touch a specified object,
Eg: Specifies the object. attachEvent ("event", execution function ).
The attachEvent () method is used as follows:
ElemObject. attachEvent ("eventName", functionReference );
The value of the eventName parameter is a string that represents the event name, such as onmousedown. The functionReference parameter is a function reference without parentheses, which is the same as the event attribute method described earlier. Therefore, for the key object in the preceding example, you can bind the function to the click Event of the key using the following script statement:
Document. getElementById ("button1"). attachEvent ("onclick", myFunc );
<Script language = "JavaScript" type = "text/javascript">
Aa = document. getElementById ('button1 ');
Function aa ()
{
Document. getElementById ("Button1"). attachEvent ("onclick", myFunc );
}
Function myFunc ()
{
Alert ("asdfsd ");
}
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Input id = "Button1" type = "button" value = "button"/>
<Input id = "Button2" type = "button" value = "button" onclick = "aa ();"/>
// Here the aa () method is to register an event for Button1
// If no method is required for registration, use document. getElementById ("Button1"). attachEvent ("onclick", myFunc );
// The returned object is null, because it is first executed from js and has not been included in the html code, so the Button1 object cannot be found.
</Form>
</Body>
</Html>
This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/faunjoe/archive/2009/07/21/4368912.aspx