$ .clone和.cloneNode [英] $.clone and .cloneNode

查看:62
本文介绍了$ .clone和.cloneNode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对jQuery $.clone 与原始 .cloneNode 属性之间的区别感到困惑.

I am a bit confused on the difference between jQuery $.clone and the raw .cloneNode property.

如果我在做

$('blah').cloneNode(true)将在jQuery空间之外创建一个全局对象.

$('blah').cloneNode(true) this will create a global object outside of the jQuery space.

如果我使用

$('blah').clone(true)将在jQuery空间内创建一个jQuery对象,但复制包括事件在内的所有内容?

$('blah').clone(true) this will create a jQuery object inside the jQuery space but copy everything including events ?

如果我使用jQuery,我应该坚持使用 .clone ,如果我将代码从 .cloneNode 更改,会不会有效果?

If I am using jQuery should I stick with .clone and if I change my code from .cloneNode will there any effect ?

推荐答案

一些事情.您在 this 而不是 $(this)上调用 cloneNode .其次,使用 cloneNode 您不能克隆与原始节点关联的事件,而使用jQuery的 clone ,它将克隆事件数据(如果设置了第一个标志).设置 clone 的第二个标记将克隆原始元素的子元素和元素.

A few things. You call cloneNode on this not $(this). Second, with cloneNode you can't clone the events associated with the original node, whereas with jQuery's clone, it clones the events and data (if the first flag is set). Setting the second flag of clone clones the original element's children and their elements.

根据您的需要进行相应的使用.

Use accordingly, according to your needs.

这篇关于$ .clone和.cloneNode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆