jQuery copy node
jQuery copy node
clone([false])Default: only copy the object node
clone(true): Copy the element node and its corresponding events, set to true to copy all event processing of the element
<!DOCTYPE html> <html> <head> <title>php.cn</title> <meta charset="utf-8" /> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <script> $(document).ready(function(){ //給#big的每個li設置click事件css() attr() //click()方法本身有遍歷機制,會為每個li都設置click事件 //每個li都會綁定一個click事件(會為每個li執(zhí)行一次click內部的function) $("#big li").click(function(){ //單擊彈出對應的文本信息 alert($(this).html()); }); }); function f1(){ //var fu_guan = $('#guan').clone(false); //只復制“節(jié)點” var fu_guan = $('#guan').clone(true); //“節(jié)點”和“其事件”都復制 $("#small").append(fu_guan); } </script> <style type="text/css"> div {width:300px; height:200px; background-color:pink;} </style> </head> <body> <ul id="big"> <li>A</li> <li>B</li> <li id="guan">C</li> </ul> <ul id="small"> <li>a</li> <li>b</li> <li id="gai">c</li> </ul> <input type="button" value="復制" onclick="f1()" /> </body> </html>