Home > Web Front-end > JS Tutorial > body text

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

coldplay.xixi
Release: 2020-11-25 16:55:51
Original
4621 people have browsed it

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

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!