是否可以在JavaScript / JQuery中克隆html元素对象? [英] Is it possible to clone html element objects in JavaScript / JQuery?

查看:140
本文介绍了是否可以在JavaScript / JQuery中克隆html元素对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找有关如何解决问题的一些提示。

I am looking for some tips on how to solve my problem.

我在表格中有一个html元素(如选择框输入字段)。现在我想复制对象并从副本中生成一个新对象,并使用JavaScript或jQuery。我认为这应该以某种方式起作用,但我现在有点无能为力。

I have a html element (like select box input field) in a table. Now I want to copy the object and generate a new one out of the copy, and that with JavaScript or jQuery. I think this should work somehow but I'm a little bit clueless at the moment.

像这样(伪代码):

oldDdl = $("#ddl_1").get(); 

newDdl = oldDdl;

oldDdl.attr('id', newId);

oldDdl.html();


推荐答案

jQuery方式(不是最有效的):

The jQuery way (not the most efficient):

查看 clone()方法of JQuery

Look at the clone() method of JQuery

使用你的代码可以做类似的事情:

Using your code you can do something like that:

$('#ddl_1').clone().attr('id', newId).appendTo('p'); // append to where you want

这篇关于是否可以在JavaScript / JQuery中克隆html元素对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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