[英]Jquery changing ID of cloned element children not working
I am trying to clone an element and then change the id of one of its children: 我正在尝试克隆一个元素,然后更改其子元素之一的ID:
var s = $('.RunWell').clone().wrap('<div>').parent().html();
s.find('#tag' + runNum).attr('id', 'tag'+ (++runNum));
but it is not working, what am I doing wrong ?? 但它不起作用,我在做什么错? how to change the ID of a child of a cloned element ? 如何更改克隆元素的子代的ID?
you don't have to go to its html..just use the cloned jquery object. 您不必转到其html ..,只需使用克隆的jquery对象即可。
try this 尝试这个
var s = $('.RunWell').clone().wrap('<div>');
s.find('#tag' + runNum).attr('id', 'tag'+ (++runNum));
the line 线
var s = $('.RunWell').clone().wrap('<div>').parent().html();
assigns variable s
with a string value. 为变量s
分配一个字符串值。 But you are assuming it to be a jquery object in the next line by performing a .find on it. 但是您通过对它执行.find来假设它是下一行中的jquery对象。
It should be 它应该是
var $s = $('.RunWell').clone().wrap('<div>').parent();
$s.find('#tag' + runNum).attr('id', 'tag'+ (++runNum));
//$s is used to denote it as a jquery object to provide more readability to code.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.