简体   繁体   中英

Hide main div in which sub div contains specific text

I'm trying to make my first Tampermoney script

Here's an example of an html page :

<div class="a">
  <div class="b">
    "Hello world"
  </div>
  <div class="n">
    "Test"
  </div>
</div>
<div class="a">
  <div class="d">
    <div class="e">
      ...
        <div class="n">
          "Hello world"
        </div>
    </div>
  </div>
</div>

I found this topic, very interesting, but I'm not able to make it fits my requirements : Hiding div that contains specific string

I would like to hide the divs class="a" ONLY if it contains a div class="n" that contains the text "Hello world". Do I need to loop on all the divs class="a", to seek for a class="n" containing "Hello world" ? I'll need some help please...

<div class="a">
  <div class="b">
    "Hello world"
  </div>
  <div class="n">
    "Test"
  </div>
</div>

I think this will help you. And you want if class .a contain .n with text("hello world") , .a need to be hide. I just make edit your HTML part and also comment out which div will be hide. According to your HTML first one will be not hide because .n contain "test". if any question or if you found anything wrong on my answer ask me. :) Live On Fiddle

UPDATE: In your Code the problem in your parent div you have 5 class _5jmm, _5pat, _3lb4, m_95yeui-j, _x72 but you write in jQuery closest('.5jmm _5pat _3lb4 m_95yeui-j _x72').hide(). So you can set only one class which is closest. And you don't

 $(window).on('load', function() { $('._5g-l:contains("Publication suggérée")').closest('._5jmm').hide(); }); 
 <script src="https://code.jquery.com/jquery-3.1.0.min.js"></script> <div data-fte="1" data-ftr="1" class="_5jmm _5pat _3lb4 m_95yeui-j _x72" id="hyperfeed_story_id_581d0a6f0b12a3832990101" data-testid="fbfeed_story" [...]> <div class="_4-u2 mbm _5v3q _4-u8" id="u_jsonp_2_1f"> <div class="_3ccb _4-u8" [...]> <div></div> <div class="userContentWrapper _5pcr" role="article" aria-label="Actualité"> <div class="_1dwg _1w_m"> <div class="_5g-l"><span>Publication suggérée</span></div> </div> </div> </div> </div> </div> 

Hmmmm, I think it needs a few ajustements... Here's my code :

$(window).load(function(){
    $('._5g-l:contains("Publication suggérée")').closest('._5jmm _5pat _3lb4 m_95yeui-j _x72').hide();
});

The web page :

在此处输入图片说明

And the associated code :

<div data-fte="1" data-ftr="1" class="_5jmm _5pat _3lb4 m_95yeui-j _x72" id="hyperfeed_story_id_581d0a6f0b12a3832990101" data-testid="fbfeed_story" [...]>
    <div class="_4-u2 mbm _5v3q _4-u8" id="u_jsonp_2_1f">
        <div class="_3ccb _4-u8" [...]>
            <div></div>
            <div class="userContentWrapper _5pcr" role="article" aria-label="Actualité">
                <div class="_1dwg _1w_m">
                    <div class="_5g-l"><span>Publication suggérée</span></div>
    [...]

Also, I can't put in the webpage :

<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>

When I try to execute the commande in the console :

$('._5g-l:contains("Publication suggérée")').closest('._5jmm _5pat _3lb4 m_95yeui-j _x72').hide();
(unknown) Uncaught Error: <![EX[["Tried to get element with id of \"%s\" but it is not present on the page.","._5g-l:contains(\"Publication suggérée\")"]]]>
    at h (https://www.facebook.com/rsrc.php/v3/yZ/r/AveNRnydIl_.js:36:166)
    at i (https://www.facebook.com/rsrc.php/v3/yZ/r/AveNRnydIl_.js:36:293)
    at <anonymous>:1:1

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