如何在 jQuery 中删除子项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11713306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:51:50  来源:igfitidea点击:

How can I remove children in jQuery?

jquery

提问by Hoa

In the following example

在下面的例子中

http://jsfiddle.net/pDsGF/

http://jsfiddle.net/pDsGF/

I want to remove just class 'child' from class 'parent'. I have tried

我想从“父母”班级中删除“孩子”班级。我试过了

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

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

But it doesn't work

但它不起作用

回答by Ry-

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

您需要句点来按类获取元素,例如。对于两个,该语法不正确。

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

Here's a demo.

这是一个演示。

回答by Besnik

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()?;

回答by Fraser

This will do the trick.

这将解决问题。

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

(minitech beat me to it :) )

(minitech 打败了我 :) )

回答by Horen

Try $('.parent').find('.child').remove();?http://jsfiddle.net/pDsGF/1/

试试$('.parent').find('.child').remove();?http://jsfiddle.net/pDsGF/1/

Edit:In case I misunderstood and you wanted to actually remove the class try $('.parent').find('.child').removeClass('child')

编辑:以防万一我误解了并且您想实际删除该课程,请尝试$('.parent').find('.child').removeClass('child')

回答by insCode

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

如果你有太多同班的孩子,那么父母对所有人来说都是不同的。您也可以通过放置子类来删除 Child

//[._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

此脚本仅删除所选父级中的所选子级。如果有许多具有相同标识符的子项,那么它们都将被删除,因此 [???] 在这种情况下,您可以创建自定义属性来选择元素 [仅适用于 HTML5]。例子

<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 ]

如果您对这篇文章有任何疑问,请告诉我 [评论]