Home  >  Article  >  Web Front-end  >  What is the difference between on() and click() in jquery

What is the difference between on() and click() in jquery

coldplay.xixi
coldplay.xixiOriginal
2020-11-25 16:55:514721browse

The difference between on() and click() in jquery: 1. [click()] belongs to static loading. When the page is loaded, click events will no longer be added for newly added elements; 2. [on ()] belongs to dynamic loading. When the page is loaded, you can add events for newly added elements, but the parent element must be selected.

What is the difference between on() and click() in jquery

  • This method is suitable for all brands of computers

jquery The difference between on() and click():

  • click() belongs to static loading. When the page is loaded, it will no longer be added. Add a click event to the element.

  • on() belongs to dynamic loading. When the page is loaded, events can be added for newly added elements. But the parent element must be selected.

Usage example:

$("#newclick").click(function(){ 
    $(".li").append(&#39;<li>动态添加的HTML元素click<button class="deleteclick">Delete</button></li>&#39;); 
});
$("#newon").click(function(){ 
    $(".li").append(&#39;<li>动态添加的HTML元素on<button class="deleteon">Delete</button></li>&#39;); 
});
$(".delete").click(function(){ 
    $(this).parent().remove(); 
}); 
$(".li").on(&#39;click&#39;, ".deleteon", function(){
    $(this).parent().remove(); 
})
$(".deleteclick").click(function(){ 
    $(this).parent().remove(); 
});

Note: The element in front of on must exist in the DOM when the page is loaded.

Related free learning recommendations: javascript (video)

The above is the detailed content of What is the difference between on() and click() in jquery. For more information, please follow other related articles on the PHP Chinese website!

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