$of ''.click versus onclick

  • 2020-03-30 04:01:48
  • OfStack

The 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>

The difference between:

1. Onclick is a binding event that tells the browser what to do when the mouse is clicked

Click itself is a method that fires an onclick event, which happens whenever the element's click() method is executed. As the appeal code shows, when the 'ee' button is clicked, the 'dd' onclick event is triggered (normally the 'dd' button is pressed to trigger the 'dd' onclick event) because


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

When the 'ee' button is clicked, the code invokes the 'dd' click() method internally, triggering the 'dd' onclick event.

The main function of the click() method is to trigger the onclick event by calling the click method element. In addition, 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, which ACTS as an append to the event. Instance as follows

The 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 popup order is 'bb' and then 'aa'.


Related articles: