Home > Web Front-end > JS Tutorial > Example introduction to the difference between $('').click and onclick_jquery

Example introduction to the difference between $('').click and onclick_jquery

WBOY
Release: 2016-05-16 16:35:18
Original
1493 people have browsed it

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>
Copy after login

Difference:

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

Click itself is a method that triggers the onclick event. As long as the click() method of the element is executed, the onclick event will be triggered. As shown in the appeal code, when the 'ee' button is clicked, the onclick event of 'dd' will be triggered (normally, you have to press the 'dd' button to trigger the onclick event of 'dd'). The reason is because

$("#btn4").click(function(){
$("#btn3").click();
});
Copy after login

When the 'ee' button is clicked, the click() method of 'dd' is called internally in the code, thus triggering the onclick event of 'dd'.

2. The main function of the click() method is to trigger the onclick event of the element that calls the click method. In addition, if the following code is defined in the click method

$("#btn3").click(function(){
alert("*****");
});
Copy after login

The function code in the click method will be executed after the onclick event is executed. At this time, the click method plays the role of appending events. 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>
Copy after login

The pop-up order of the pop-up boxes is first 'bb', then 'aa'.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template