19

在下面的例子中

http://jsfiddle.net/pDsGF/

我想从“父”类中删除“子”类。我试过了

.remove($('parent').children('child'))​</p>

但它不起作用

4

5 回答 5

28

You need periods to get elements by class, for one. For two, that syntax isn't correct.

$('.parent .child').remove();

Here's a demo.

于 2012-07-29T21:59:28.230 回答
10

Do you want to remove the childs (with a class "child") of parents (with a class "parent")?

$('.parent').children('.child').remove();

Or simply:

$('.parent .child')​.remove()​;
于 2012-07-29T22:04:14.320 回答
3
于 2012-07-29T21:59:07.667 回答
2

这会成功的。

$('.parent .child').remove();

(minitech 打败了我 :) )

于 2012-07-29T21:59:07.213 回答
1

many ways: if you have no identifier for the child then you can remove child my its position on the parent :

var p ='.parrent';//identify the parrent
   $('p').children('1').remove();//remove the child placed in ('1');

remove Directly [ if you have a identifier ]

$('.parent .child').remove();//it removes child from the parent.

if you dont know what the parent is.

var selector = '.child';//you must identify ONE child to find its parent

var sP = $(selector).parent();//selecting the parent for this Child

//now removing the Child [identifier = position of child]

$(select).parent().children("5").remove();

In case if you have too many child with same class then but the parent is different for all. you can remove the Child also by putting the child class

//[._iNote] is the selector for the removing Element Here.
$(select).parent().children("._iNote").remove();

This script only remove selected child in the the selected parrent . if there is many child with same identifier then They all will be removed so [???] in this kind of case you can create custome attr for selecting the element [only for HTML5 ]. example

<p data-me="someThing-i-like"> [its a custom attr (data-me="someThing-i-like")] </p>

in this case For removing this element,

$("[data-me=someThing-i-like]").remove();// will work fine

if you have any quistion about this post plz plz plz let me know [ comment ]

于 2015-11-19T05:32:16.827 回答