jQuery将DIV复制到另一个DIV中 [英] jQuery duplicate DIV into another DIV

查看:111
本文介绍了jQuery将DIV复制到另一个DIV中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要一些jQuery帮助将DIV复制到另一个DIV中,并希望这是可能的。我有以下HTML:

Need some jquery help copying a DIV into another DIV and hoping that this is possible. I have the following HTML:

  <div class="container">
  <div class="button"></div>
  </div>

然后我在页面的另一个位置有另一个DIV,我想复制'button 'div转换为以下'package'div:

And then I have another DIV in another location in my page and I would like to copy the 'button' div into the following 'package' div:

<div class="package">

Place 'button' div in here

</div>


推荐答案

您需要使用 clone()方法来获取元素的深层副本:

You'll want to use the clone() method in order to get a deep copy of the element:

$(function(){
  var $button = $('.button').clone();
  $('.package').html($button);
});

完整演示: http ://jsfiddle.net/3rXjx/

jQuery docs
$ b

From the jQuery docs:


.clone()方法执行一组匹配的
元素的深层副本,意思是它复制匹配的元素以及其后代元素和文本节点的所有
。当将
和其中一种插入方法结合使用时,.clone()是一种方便的方式,可以在页面上
重复元素。

The .clone() method performs a deep copy of the set of matched elements, meaning that it copies the matched elements as well as all of their descendant elements and text nodes. When used in conjunction with one of the insertion methods, .clone() is a convenient way to duplicate elements on a page.

这篇关于jQuery将DIV复制到另一个DIV中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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