javascript jquery 删除特定的孩子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6112608/
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-10-25 19:31:21  来源:igfitidea点击:

jquery remove specific children

javascriptjquery

提问by aWebDeveloper

I have the following code :

我有以下代码:

$('.TopNotificationIcon span').remove();    

Can I replace .TopNotificationIconwith thisi.e only spanexists inside this specific class.

我可以替换.TopNotificationIconthisie 只span存在于这个特定的类中吗?

This is the structure

这是结构

<div class="TopNotificationIcon"><span>xxxxx</span></div>

On click of .TopNotificationIcon, spanshould be removed.

单击 时.TopNotificationIconspan应将其删除。

回答by Jishnu A P

if you have click event for .TopNotificationIconyou can do something like this

如果你有点击事件,.TopNotificationIcon你可以做这样的事情

$('.TopNotificationIcon').click(function(){
    $('span',this).remove();    
});

回答by Code Maverick

I would use the find() method, as it seems to be the fastest:

我会使用 find() 方法,因为它似乎是最快的:

$("div.TopNotificationIcon").click(function() {

    $(this).find("span").remove();    

});

回答by Matt

Yes but youd need to change the line to:

是的,但您需要将该行更改为:

$(this).children('span').remove();

js fiddle: http://jsfiddle.net/UNhhh/1/

js小提琴:http: //jsfiddle.net/UNhhh/1/

回答by Brice Favre

If you want to remove all span under TopNotification you can do this :

如果要删除 TopNotification 下的所有跨度,可以执行以下操作:

$('div').live('click', function(){
    $(this).children('span').remove();    
});

It will remove all children in a div.

它将删除 div 中的所有子项。

回答by MNIK

Try this...

试试这个...

$('span').remove('.TopNotificationIcon');

This will remove all span elements with the class TopNotificationIcon and also child elements

这将删除所有具有 TopNotificationIcon 类的 span 元素以及子元素