Home>Article>Web Front-end> What are the differences between on and click in jquery?
Below I will share with you a detailed explanation of the difference between on and click based on jquery. It has a good reference value and I hope it will be helpful to everyone.
When using jQuery to write js, some use on to write behavior functions, and some use click. It is important to understand the difference between the two.
The following is the HTML code used to test the difference between the two.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
展示jQuery中on()和click()的区别
点击生成新按钮。NewOn生成的Delete按钮行为用on()实现,NewClick生成的Delete按钮行为用click()实现。
js file is as follows:
$("#newclick").click(function(){ $(".li").append('
After the page is loaded, click the NewOn and NewClick buttons, and the page will appear as shown below.
Phenomena:
The original HTML element will be deleted when you click the Delete button behind it. As for dynamically added HTML elements, using the click() method, they cannot be deleted by clicking the Delete button; they can be deleted using the On() method.
Reason:
element.click() does not support binding events to dynamic elements or styles. Support for binding events to dynamic elements is .live() and .on(). Live is no longer recommended after jQquery 1.7. When using .on(), please note that the element before on must exist in the DOM when the page is loaded.
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
How to deal with display issues before vue rendering (detailed tutorial)
Crawling in Node.js Douban Data (detailed tutorial)
About the loading order of rendering and plug-ins in Vue (detailed tutorial)
Implemented through AjaxUpLoad.js Code example for file upload (detailed tutorial)
Detailed introduction to the use and functions of the JsChart component
The above is the detailed content of What are the differences between on and click in jquery?. For more information, please follow other related articles on the PHP Chinese website!