jQuery | clone()及其示例

这个 克隆() 是jQuery中的内置方法,用于复制选定元素,包括其子节点、文本和属性。 语法:

null
$(selector).clone(true|false)

参数: 它接受一个可选参数,该参数可以是true,也可以是false,用于指定是否应复制事件处理程序。 返回值: 它返回选定元素的克隆元素。

显示此方法工作的jQuery代码:
代码#1: 在下面的代码中,不会将true或false传递给change方法。

< 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 >


单击“单击我”按钮之前- 图片[1]-jQuery | clone()及其示例-yiteyi-C++库

点击“点击我”按钮后- 图片[2]-jQuery | clone()及其示例-yiteyi-C++库

代码#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就会工作,这也会反映在克隆的元素上。 输出: 单击“单击我”按钮之前- 图片[3]-jQuery | clone()及其示例-yiteyi-C++库

点击“点击我”按钮后- 图片[4]-jQuery | clone()及其示例-yiteyi-C++库

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享