克隆一个div并将其ID及其所有子代的ID更改为唯一 [英] Clone a div and change the ID's of it and all it's children to be unique

查看:114
本文介绍了克隆一个div并将其ID及其所有子代的ID更改为唯一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JQuery可以克隆这样的Div并更改其ID和所有子ID的值,然后添加新的标识符吗?

Using JQuery it possible to clone a Div like this and change add a new identifier to it's ID and all it's children ID's?

例如,我希望能够克隆它:

For instance I'd like to be able to clone this:

<div id="current_users">
<table id="user_list">
<tr id="user-0">
<td id="first_name-0">Jane</td>
<td id="last_name-0">Doe</td>
</tr>
<tr id="user-1">
<td id="first_name-1">John</td>
<td id="last_name-1">Doe</td>
</tr>
</table>
</div>

看起来像这样:

<div id="current_users_cloned">
<table id="user_list_cloned">
<tr id="user-0_cloned">
<td id="first_name-0_cloned">Jan</td>
<td id="last_name-0_cloned">Doe</td>
</tr>
<tr id="user-1_cloned">
<td id="first_name-1_cloned">John</td>
<td id="last_name-1_cloned">Doe</td>
</tr>
</table>
</div>

还是我应该重新考虑结构并使用rel属性和可重用的类声明?

Or should I just rethink my structure and use rel attributes and reusable class declarations?

谢谢

Tegan Snyder

Tegan Snyder

推荐答案

$("#current_users").clone(false).find("*[id]").andSelf().each(function() { $(this).attr("id", $(this).attr("id") + "_cloned"); });

并观看您的活动(如果有).如果它们依赖id,则必须对其进行修复.

And watch your events if there are any attached. You'll have to fix them up if they rely on ids.

这篇关于克隆一个div并将其ID及其所有子代的ID更改为唯一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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