Home  >  Article  >  Web Front-end  >  Example introduction to the difference between $("").click and onclick_jquery

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

WBOY
WBOYOriginal
2016-05-16 16:35:181461browse

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 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();
});

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("*****");
});

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>

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

Statement:
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