$ (""). The difference between click and onclick example Introduction _jquery

Source: Internet
Author: User

HTML code

<script type= "Text/javascript" > 
$ (function () { 
$ ("#btn4"). Click (function () { 
$ (#btn3). Click () ; 
}); 
}); 
function Change () { 
alert ("onclick"); 
} 
</script> 

<button id= "btn3" onclick= "Change ()" >dd</button> 
<button id= "Btn4" >ee </button>

Difference:

1.onclick is the binding event that tells the browser what to do when the mouse clicks

The click itself is a method that triggers the onclick event, which triggers the onclick event whenever the element's click () method is executed. As the appeal code shows, clicking the ' EE ' button triggers the ' dd ' onclick event (normally the ' DD ' button triggers the ' dd ' onclick event), because

$ ("#btn4"). Click (function () {
$ ("#btn3"). Click ();
};

Clicking the ' EE ' button invokes the ' DD ' click () method inside the code, triggering the ' DD ' onclick event.

The primary role of the 2.click () method is to trigger the call to the click Method Element onclick event. Also, if the following code is defined in the click Method

$ ("#btn3"). Click (function () {
alert ("* * *");
});

The function code in the click Method executes after the OnClick event has finished executing, at which point the click Method acts as an append event. Examples are as follows

HTML code

<script type= "Text/javascript" > 
$ (function () { 
$ ("#btn3"). Click (function () { 
alert ("AA"); 
}) 
; 
function Change () { 
alert ("BB"); 
} 
</script> 
<button id= "btn3" onclick= "Change ()" >dd</button>

The pop-up sequence is first ' BB ' and then ' AA '.

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.