Adding new elements with jquery is easy, and the element events that are added dynamically in the face of jquery append do not work. In the On method, you first find the original selector (example. info), and then find the dynamically added selector (such as column. Delete).
Not much to say, please look at the code below.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>测试</title><script type="text/javascript"src="../resources/js/jquery-1.11.1.min.js"></script><script type="text/javascript">$(function(){//动态添加$(".add").on("click",function(){console.log("进来了");$(".info").append(‘<div class="delete">);}); //对动态添加的元素添加事件-删除$(".info").on("click",".delete",function(){console.log("进来了Delete!");$(this).remove();}); //关键代码 }); </script> <body>
‘add‘>单击我添加动态元素 <div class="info"></div> </body>
jquery append dynamically added element event on not working solution