Use the attachEvent () method in js

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.