Hide div in specific pages with jquery

Asked

Viewed 554 times

-1

Good morning,

I have a problem to hide a div only on a specific page, in case you have a php file with include_once that includes in the pages the header, but has a specific page that does not need a div that is inside the header and I want to hide, how could I do this with jquery.

  • You’ll get better answers by giving people code they can use to reproduce the problem. https://answall.com/help/mcve

  • Post the code please... Theoretically, you could search by ID, Class, etc. But each case is a case. Post the code...

  • 1

    Sending a div unnecessarily via PHP to then hide in JS seems really strange (read gambiarra). Wouldn’t it be wiser to determine this on the same server? Or would it have some real justification for this, to better understand?

  • Gambiarra by Gambiarra... because only on the page you do not want the div you do not put in <head> one <style> with display:None in #suadiv. So you don’t even need jQuery...

  • Easier would be in include to put IF path!= such { <html of div> } in php itself

  • 1

    I usually name the pages, putting at the beginning, before the <html tag>: $pagename = "home";... so it is easy to display or hide a specific content according to the variable $pagename.

Show 1 more comment

1 answer

2


$(function(){
  if (window.location.pathname == "meusite/url1php"||window.location.pathname == "meusite/diretorio/url2.html"||window.location.pathname == "url3.html") {
        $('#umadiv').hide();
  } else {
        $('#umadiv').show();
  }
});

Scriptless

You can also define a class in the element <body> indicating the current page. So if you are on the index page, <body class='index'> you can use CSS to hide specific elements.

body.index #umadiv { display: none; }

Example:

body.index #umadiv { display: none; };
<body class='index'>

<div id="umadiv">
kkkkkkk
</div>
Conteudo da pagina
</body>

You can also use the following scheme:

index php.

  <php
    $pagina="index";
    .......
    include_once "header.php";
    .......

page1.php

  <php
    $pagina="pagina1";
    .......
    include_once "header.php";
    .......

page2.php

  <php
    $pagina="pagina2";
    .......
    include_once "header.php";
    .......

etc......

header.php

  $ocultar = array("index", "pagina1", "pagina2"); 
  if (!in_array($pagina, $ocultar)) { 

      //a div aqui

  }

Browser other questions tagged

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