[英]Select element inside a cloned element variable
I have cloned an element using clone()
: 我已经使用clone()
克隆了一个元素:
var clone = $('#orig').clone();
The clone works fine, but I having some trouble trying to select elements inside it by ID. 克隆工作正常,但是尝试通过ID选择其中的元素时遇到了一些麻烦。 All nested elements have the same ID as the original ones, and I need to manipulate some before appending them to the page... 所有嵌套元素都具有与原始元素相同的ID,在将它们附加到页面之前,我需要进行一些操作...
I am trying something like this for example: 我正在尝试类似这样的事情:
alert(clone.filter("#Full").attr('id'));
Could you help me on that? 你能帮我吗?
Try this way 试试这个
var clone = $('#orig').clone();
clone.attr('id','orig1');
check it here http://jsfiddle.net/3tWks/ 在这里检查http://jsfiddle.net/3tWks/
Firstly, use classes instead of IDs. 首先,使用类而不是ID。 IDs should always be unique within a document, and may result in unpredictable behaviour if cloned (if you want the cloned elements to have IDs, by all means assign new ones when you clone!) ID在文档中应该始终是唯一的,并且在克隆时可能会导致无法预测的行为(如果希望克隆的元素具有ID,则在克隆时一定要分配新的ID!)
That said, you want to be using .find
instead of .filter
to find elements nested within your clone element: 就是说,您想使用.find
而不是.filter
来查找嵌套在克隆元素中的元素:
clone.find(".myClassName")
/* or */
clone.find("#Full")
The snippet you provided in your question works fine! 您在问题中提供的代码片段效果很好!
Just replace filter
with find
. 只需将filter
替换为find
。
See this fiddle: http://jsfiddle.net/Pkv7S/ 看到这个小提琴: http : //jsfiddle.net/Pkv7S/
However, yes, you should be wary of duplicate IDs. 但是,是的,您应该警惕重复的ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.