0

I am trying to clear all classes and ids of an element and its children. But my current code only clearing classes and ids only from the parent.

Please tell me how I clear all classes and ids of an element and its child.

Here is my code:

jQuery('#menu-main-menu').clone()
.removeAttr('id').removeAttr('class').prependTo('body');
4

3 回答 3

2

This will process all subordinate objects of the cloned tree:

jQuery('#menu-main-menu').clone().find("*")
    .removeAttr('id').removeAttr('class').end().prependTo('body');

If you also want to remove the attributes of the new parent, you can do this:

var clone = jQuery('#menu-main-menu').clone();
clone.add(clone.find("*")).removeAttr('id').removeAttr('class');
clone.prependTo('body');

A point to understand is that.add() creates a new jQuery object so the clone jQuery object is not affected by the .add().

于 2013-06-04T04:24:59.703 回答
1

You must remove id and class from childrens too:

var $elem = jQuery('#menu-main-menu').clone();
$elem.removeAttr('id').removeAttr('class');
$elem.find('*').removeAttr('id').removeAttr('class');
$elem.prependTo('body');
于 2013-06-04T04:25:34.253 回答
0

Try:

jQuery('#menu-main-menu').clone().children().
.removeAttr('id').removeAttr('class').prependTo('body');
于 2013-06-04T04:27:50.230 回答