jQuery克隆重复的ID [英] jQuery clone duplicate IDs

查看:168
本文介绍了jQuery克隆重复的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大量无序列表的HTML元素。我需要将这个元素克隆到页面中其他地方,添加不同的样式(这很简单,使用jQuery)。

I have an HTML element with a large collection of unordered lists contained within it. I need to clone this element to place elsewhere on the page with different styles added (this is simple enough using jQuery).

$("#MainConfig").clone(false).appendTo($("#smallConfig"));

然而,问题在于所有列表及其关联列表项都有ID和 clone 复制它们。有没有一种简单的方法可以在追加前使用jQuery替换所有这些重复的ID?

The problem, however, is that all the lists and their associated list items have IDs and clone duplicates them. Is there an easy way to replace all these duplicate IDs using jQuery before appending?

推荐答案

如果您需要一种方法来引用列表在克隆它们之后的项目,您必须使用类,而不是ID。将所有id =...更改为class =...

If you need a way to reference the list items after you've cloned them, you must use classes, not IDs. Change all id="..." to class="..."

如果您正在处理遗留代码或其他内容,并且无法将ID更改为类,您必须在追加前删除id属性。

If you are dealing with legacy code or something and can't change the IDs to classes, you must remove the id attributes before appending.

$("#MainConfig").clone(false).find("*").removeAttr("id").appendTo($("#smallConfig"));

请注意,您无法再引用单个项目。

Just be aware that you don't have a way to reference individual items anymore.

这篇关于jQuery克隆重复的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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