Three methods for writing Javascript event processing code

Source: Internet
Author: User

1. event handler as the HTML property value


HTML code
<Input name = "btn" type = "button" id = "btn" value = "button" onclick = "alert ('mzwu. com');"/>
 

 

2. event handler as a JavaScript property

Webpage tutorial Network


HTML code
<Input name = "btn" type = "button" id = "btn" value = "button"/>
<Script language = "javascript">
Document. getElementById ("btn"). onclick = function () {alert ("mzwu.com ");}
</Script>
 

 

Note: The first method may mislead you to write it as document. getElementById ("btn "). onclick () = "alert ('mzwu. com '); ", this write is incorrect! In fact, the first method implicitly creates a function, which can be described using the following code, because it detects that the HTML attribute value is actually a function:

Webjx. Com


HTML code
<Input name = "btn" type = "button" id = "btn" value = "button" onclick = "alert (typeof (this. onclick);"/>
 

Webpage tutorial Network

 

The above Code uses a direct volume to create a function and changes it to the function () Statement:


HTML code
<Input name = "btn" type = "button" id = "btn" value = "button"/>
<Script language = "javascript">
Function e ()
{
Alert ("mzwu.com ");
}
Document. getElementById ("btn"). onclick = e;
</Script>
 

Webpage tutorial Network

 

Therefore, we can extend the third method.

Webjx. Com


3. Method 3

Webjx. Com


HTML code
<Input name = "btn" type = "button" id = "btn" value = "button"/>
<Script language = "javascript">
Function document. all. btn. onclick ()
{
Alert ("mzwu.com ");
}
</Script>
 


Note: such methods include function document. onclick () {}, but this method is only feasible in IE and FF tests, and then changed to function document. getElementById ("btn "). onclick () {} does not work in IE or FF. It is not difficult to explain the lack of identifiers, because the JavaScript identifiers do not allow dot numbers. document is used. getElementById ("btn "). the onclick identifier is obviously incorrect, but the document. all. btn. onclick is a bit confusing.

 

Related Article

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.