Clear body keeping only 2 elements

Asked

Viewed 34 times

3

I need to delete everything from body, except for 2 items.

I tried the ways below but none worked:

  body > *:not("div#pedidosRelMensal, h1.resposta"){
    display: none;
  }

  body > *:not("div#pedidosRelMensal")not("h1.resposta"){
    display: none;
  }

  body > *:not(#pedidosRelMensal, h1.resposta){
    display: none;
  }

If that’s all I do:

 body > *:not(#pedidosRelMensal){
    display: none;
  }

Cool.

But I also need h1.

1 answer

4


The right thing would be to concatenate the two :not():not() and within the not() is not used "" in the name of the elements.

body > *:not(#pedidosRelMensal):not(.resposta) {} 

That way it works

body > *:not(#pedidosRelMensal):not(.resposta) {
    display: none;
}
<div id="pedidosRelMensal">
    div id pedidosRelMensal
</div>
<h1 class="resposta">H1 Resposta</h1>

<div>
    div qualquer
</div>
<h1>h1 qualquer</h1>

Another way would be to erase everything and then come back with what you need.

body > *{
    display: none;
}
#pedidosRelMensal,
.resposta {
    display: block;
}
<div id="pedidosRelMensal">
    div id pedidosRelMensal
</div>
<h1 class="resposta">H1 Resposta</h1>

<div>
    div qualquer
</div>
<h1>h1 qualquer</h1>

  • You bore, I was already answering.

  • kkkk, reponde ae tb @Andersoncarloswoss

  • @Andersoncarloswoss but I missed! : / I’ll even edit...

  • In CSS I wouldn’t know how to do it, but in JS I would do it like this: let printContents = document.getElementById('print').innerHTML;&#xA; document.body.innerHTML = printContents;

  • 1

    @Andersoncarloswoss now yes, concatenating the :not():not() One does not cancel the other. But collaborate there, the more better :d

  • But I can only do it by taking one element

  • 1

    @Edwardramos com JS deve dar para fazer tranquilamente tb, the problem is that the JS bar the page print, the CSS no, so in theory for this case the CSS will be faster than the JS. With JS depending vc might even see the elements on the screen for a fraction of a second, before they disappear

  • 1

    foma 1 solved for me, Thank you!

  • @Carlosrocha cool that solved there! Good luck on the project!

  • 1

    Thanks, it’s ready now. That’s all we needed! But it will only be on localhost! Could have done it in Visuastudio or Delphi., but for this client I preferred in PHP even! Thanks for the help!

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.