简体   繁体   中英

How to remove a div if it contains a certain string in it?

there's a list of items on a page, some blank, with one example being:

<div class="item itemask">
  <div class="tophead">
    <div class="itemnumber">30</div>

    <a class="article" href=""></a>
  </div>
  <div class="bottomhead"> points by <a class="userlink" rel=""></a> ago&nbsp;&nbsp;&nbsp; <a href="/item?id=">discuss</a>
</div>

Hence, what jQuery or JavaScript could be used to find an instance of: <a class="article" href=""> in any instance of a div class=item on the page, and then delete or hide that individual parent div of class=item which contains that piece of code?

Updated answer: Based on clarification in comment, try this instead:

$('div.item:has(a.article[href=""])').remove();​

Or this should also work:

$('div.item:has(a.article:not([href]))').remove();​

Or if you have issues with either of those, try this:

$('div.item a.article').filter(function() {
    return $.trim(this.innerHTML) == '';
}).closest('div.item').remove();​​​​​​​

Original answer:

Using jQuery:

 $('div.item:has(a.article)').remove(); 

This will select <div> elements with the class item that has a descendent <a> element with the class article , and remove them.

Of course, you could use .hide() instead if you like:

 $('div.item:has(a.article)').hide(); 

If you really meant that you only want to remove the actual parent of the a.article , then you would do this:

 $('div.item :has( > a.article)').remove(); 

This will remove the parent of a.article instead of the one with the .item class. Wasn't sure which you meant.

To hide:

$("div.item a.article[href='']").parents(".item").css('display','none');

To delete:

$("div.item a.article[href='']").parents(".item").remove();

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