jquery append 動態新增的元素事件on 不起作用的解決方案
阿新 • • 發佈:2019-01-23
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <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"><h3>單擊我來刪除我</h3></div>'); }); //對動態新增的元素新增事件-刪除 $(".info").on("click",".delete",function(){ console.log("進來了Delete!"); $(this).remove(); }); }); </script> </head> <body> <h2 class='add'>單擊我新增動態元素</h2> <div class="info"></div> </body> </html>