Three ways to bind events in JavaScript

Source: Internet
Author: User

JavaScript makes Web pages friendly to users, and there are three ways to bind when using JS for time binding.

The first: beginners and general wording

1 <DivID= "Dom0">2     <inputtype= "button"value= "Click0"onclick= "dom0 ();" />3 </Div>4 <Script>5     functiondom0 () {6 Alert ('Dom0 Leval');7     }8 </Script>

The second type: programmers often use the notation

1 <DivID= "Dom1">2     <inputtype= "button"value= "Click1" />3 </Div>4 <Script>5     vardom1=document.getElementById ('dom1'). Firstelementchild;6 Dom1.onclick=function(){7 Alert ('Dom1 Leval');8     }9 </Script>

The third type: uncommon but will encounter in the interview

1 <DivID= "Dom2">2     <inputtype= "button"value= "Click2" />3 </Div>4 <Script>5     varDom2=document.getElementById ('Dom2'). Firstelementchild;6 Dom2.addeventlistener ('Click',function () {7 Alert ('Dom2 Leval');8     },false);9 </Script>

In the third notation,AddEventListener(event, function, False | | True), false represents the bubbling model, and true represents the capture model. The default is False.

Originality is not easy, respect copyright. Reprint Please specify source:http://www.cnblogs.com/xsmile/

Three ways to bind events in JavaScript

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.