这个 克隆() 是jQuery中的内置方法,用于复制选定元素,包括其子节点、文本和属性。 语法:
null
$(selector).clone(true|false)
参数: 它接受一个可选参数,该参数可以是true,也可以是false,用于指定是否应复制事件处理程序。 返回值: 它返回选定元素的克隆元素。
< html > < head > jquery/3.3.1/jquery.min.js"></ script > <!--In this example no parameter is passing to the clone method--> < script > $(document).ready(function() { $("button").click(function() { $("p").clone().appendTo("body"); }); }); </ script > </ head > < body > < p >Welcome to</ p > < p >GeeksforGeeks !!!</ p > <!--click on this method and see the clone element--> < button >Click Me!</ button > </ body > </ html > |
单击“单击我”按钮之前-
点击“点击我”按钮后-
代码#2: 在下面的代码中,true被传递给clone方法。
< html > < head > jquery/3.3.1/jquery.min.js"></ script > <!--here clone method is called with the true value passing--> < script > $(document).ready(function() { $("button").click(function() { $("body").append($("p:first").clone(true)); }); $("p").click(function() { $(this).animate({ fontSize: "+=1px" }); }); }); </ script > </ head > < body > < p >GeeksforGeeks !</ p > < p >Hello Writer !</ p > <!--click on this method and see the clone element--> < button >Click Me!</ button > </ body > </ html > |
在这个例子中,当你点击“GeekSforgeks”时,代码事件处理程序animate就会工作,这也会反映在克隆的元素上。 输出: 单击“单击我”按钮之前-
点击“点击我”按钮后-
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END