克隆一个div并将其ID及其所有子代的ID更改为唯一

编程入门 行业动态 更新时间:2024-10-25 10:32:19
本文介绍了克隆一个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更改为唯一

本文发布于:2023-11-24 05:26:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624112.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子代   并将其   div   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!