We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
title: JS解惑-jQuery.clone date: 20161107104059 categories: [WEB] tags: [js]
关于jQuery中clone的一些解惑。
假如你想把一个html元素copy一份到另一个元素中,就可以使用 .clone 了。
.clone
比如:
<div class="container"> <div class="hello">Hello</div> <div class="goodbye">Goodbye</div> </div>
如果你想把 .hello 放到 .goodbye 中,那么以下写法就是不对的。
.hello
.goodbye
$( ".hello" ).appendTo( ".goodbye" );
这样的结果将是:
<div class="container"> <div class="goodbye"> Goodbye <div class="hello">Hello</div> </div> </div>
但是,如果你使用 clone() ,那么结果将是你预期的。
clone()
$( ".hello" ).clone().appendTo( ".goodbye" );
<div class="container"> <div class="hello">Hello</div> <div class="goodbye"> Goodbye <div class="hello">Hello</div> </div> </div>
注意: jquery中的clone,只针对的是 html 原型而言的!如果你将一个js对象clone(),就会报错了。
html
jquery.min.js:2 Uncaught TypeError: Cannot read property 'ownerDocument' of undefined(…)
$.clone() 可以传递2个参数:
$.clone()
这里就不区分jQuery的版本了,假设我们都 > 1.5。如果还有低版本的,可以看看 官网 的兼容性说明。
$.clone()
数据
data-*
$.clone(true,false)
$.clone(true)
$.clone(true,true)
<p class="wrapper"> <input class="test" type="text" oninput="inputEvt(this)"/> </p> <script src="http://code.jquery.com/jquery-1.6.1.js"></script>
function inputEvt(obj){ console.log($(obj).val()) }; $('.wrapper').click(function () { console.log('click'); }); $('.test').data('obj','test'); //clone1 $('.wrapper').clone(false).prependTo($("body")); //clone2 $('.wrapper').clone(true,false).prependTo($("body")); //clone3 $('.wrapper').clone(true).prependTo($("body"));
结果说明:
click
oninput
data
clone()方法其实包含了3部分内容:
clone()
+
而以上3部分内容,分别对应以下用法:
处于性能的考虑 textarea 和 select 这2个标签动态增加的属性有点例外。
textarea
select
textara
option
如果你把一个含有ID的元素进行克隆,那么克隆以后就会出现ID重复!
为了避免这问题,建议你这么做:
class
id
(全文完)
The text was updated successfully, but these errors were encountered:
sunmaobin
No branches or pull requests
title: JS解惑-jQuery.clone
date: 20161107104059
categories: [WEB]
tags: [js]
关于jQuery中clone的一些解惑。
用途
假如你想把一个html元素copy一份到另一个元素中,就可以使用
.clone
了。比如:
如果你想把
.hello
放到.goodbye
中,那么以下写法就是不对的。这样的结果将是:
但是,如果你使用
clone()
,那么结果将是你预期的。注意: jquery中的clone,只针对的是
html
原型而言的!如果你将一个js对象clone(),就会报错了。API
$.clone()
可以传递2个参数:分析
$.clone()
:仅仅克隆当前元素的html结构以及当前元素及其子元素html上绑定的事件和数据(下文解释);数据
是指通过data-*
属性赋值的属性。$.clone(true,false)
:克隆当前元素html结构、当前元素及其子元素html上绑定的事件和数据,但是不包括子元素上动态绑定的事件和数据;$.clone(true)
:克隆当前元素html结构、当前元素及其子元素html上绑定的事件和数据,以及子元素的事件和数据。$.clone(true,true)
,因为通过API我们知道,第2个参数默认等于第1个参数。示例
结果说明:
click
);oninput
)data
)click
);oninput
)data
)click
);oninput
)data
)总结
clone()
方法其实包含了3部分内容:+
动态绑定到html上的事件和数据(直接绑定,live/delegate/on等委托绑定除外)+
子元素及其事件和数据而以上3部分内容,分别对应以下用法:
特例
处于性能的考虑
textarea
和select
这2个标签动态增加的属性有点例外。textara
克隆后,值不会带过去;select
选中的option
克隆后,依然是未选择状态;副作用
如果你把一个含有ID的元素进行克隆,那么克隆以后就会出现ID重复!
为了避免这问题,建议你这么做:
class
而非id
;id
;(全文完)
参考
The text was updated successfully, but these errors were encountered: