Home > Article > Web Front-end > What is the difference between on() and click() in jquery
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.
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('<li>动态添加的HTML元素click<button class="deleteclick">Delete</button></li>'); }); $("#newon").click(function(){ $(".li").append('<li>动态添加的HTML元素on<button class="deleteon">Delete</button></li>'); }); $(".delete").click(function(){ $(this).parent().remove(); }); $(".li").on('click', ".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!