.clone( [withDataAndEvents] ) .clone( [withDataAndEvents,] [deepWithDataAndEvents] )
<html> <head> <title>ThaiCreate.Com jQuery Tutorials</title> <script type="text/javascript" src="jquery-1.6.4.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("b").clone().prependTo("p"); }); </script> </head> <body> <b>Hello</b><p>, how are you?</p> </body> </html>
<html> <head> <title>ThaiCreate.Com jQuery Tutorials</title> <style> #orig, #copy, #copy-correct { float: left; width: 20%; } </style> <script type="text/javascript" src="jquery-1.6.4.js"></script> <script type="text/javascript"> $(document).ready(function(){ // sort order is not guaranteed here and may vary with browser $('#copy').append($('#orig .elem') .clone() .children('a') .prepend('foo - ') .parent() .clone()); // correct way to approach where order is maintained $('#copy-correct') .append($('#orig .elem') .clone() .children('a') .prepend('bar - ') .end()); }); </script> </head> <body> <div id="orig"> <div class="elem"><a>1</a></div> <div class="elem"><a>2</a></div> <div class="elem"><a>3</a></div> <div class="elem"><a>4</a></div> <div class="elem"><a>5</a></div> </div> <div id="copy"></div> <div id="copy-correct"></div> </body> </html>
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท