简体   繁体   中英

jquery remove specific children

I have the following code:

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

Can I replace .TopNotificationIcon with this ie only span exists inside this specific class.

This is the structure

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

On click of .TopNotificationIcon , span should be removed.

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

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

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

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

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

});

Yes but youd need to change the line to:

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

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

Try this...

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

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

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

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

It will remove all children in a div.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM