Clone a div and change the ID's of it and all it's children to be unique

后端 未结 3 1946
遇见更好的自我
遇见更好的自我 2020-12-10 11:09

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 clon

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-10 11:21

    David Morton had the right answer back in 2010, but I just wanted to update it. jQuery removed .andSelf() in 3.0.0 (see adeneo's answer). The latest and greatest (as of this writing) is .addBack(). So the line would be:

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

    If you need to get it back as a string, which was the case for me, add .get(0).outerHTML; to the end.

提交回复
热议问题