jquery remove list item where .text() = 'blabla'

后端 未结 3 930
后悔当初
后悔当初 2020-12-11 01:31

I have the following structure

  • something
  • ...
  • blabla
  • <
相关标签:
3条回答
  • 2020-12-11 01:56
    $('li > a:contains("blabla")').remove();
    

    Have a look at the :contains selector.

    I've just noticed that :contains does partial matching. You may need to do...

    $('li > a:contains("blabla")').each(function() {
         if ($(this).text() === 'blabla') {
             $(this).parent().remove();
         }
    });
    

    You could also make the selector less strict if doing it that way.

    ... or you could do it much neater like Nick Craver.

    0 讨论(0)
  • 2020-12-11 01:56

    If you want to avoid removing lis containing blabla unless blabla is further nested, use this:

    $("ul li > a:contains('blabla')").parent().remove();

    Otherwise, the first li from this HTML will be removed even though "blabla" is not where you might be targeting it:

    <ul>
        <li>
            <div>
                Some nested content here:
                <span>
                    Blablabla
                </span>
            </div>
        </li>
        <li>Some text</li>
    </ul>
    
    0 讨论(0)
  • 2020-12-11 02:01

    If you want a contains (substring match) then :contains() works:

    $('li:contains("blabla")').remove();
    

    If you want an exact match, e.g. not matching "blablabla", you can use .filter():

    $('li').filter(function() { return $.text([this]) === 'blabla'; }).remove();
    
    0 讨论(0)
提交回复
热议问题