有没有办法使用JQuery克隆元素? [英] Is there a way to clone elements using JQuery?

查看:98
本文介绍了有没有办法使用JQuery克隆元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用JQuery克隆HTML元素?

Is there a way to clone HTML elements using JQuery?

但是,我希望能够为克隆元素分配新属性id,name。因此,如果我有一个textfield元素,我希望将其克隆其值并将更改其id和name属性克隆为exmple2(如果它之前已命名为example)。

However, I want to be able to assign the cloned elements new attributes "id, name". So if I had a textfield element, I want to clone it without its value and change its id and name attribute to "exmple2" if it was named "example" previously.

我将非常感谢您对此以及我可用于向页面上已存在的html表单添加更多元素的任何其他实现的任何帮助。

I would appreciate any help on this and any other implementation I can use to add more elements to my html form that already exists on the page.

全部谢谢

推荐答案

一旦你有克隆元素,你可以任何你想要的方式改变它至

Once you have a cloned element you can change it any way you want to

已更新:未注意到您要更改名称和ID。

Updated: Didn't notice that you wanted to change both name and id.

$("#example").clone()
             .attr({"id":"example2", "name":"example2"})
             .html("new content")
             .appendTo("#container");

这篇关于有没有办法使用JQuery克隆元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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