简体   繁体   中英

jQuery show element ID only on homepage

I have this jQuery code on my site

if(window.location.href.indexOf("mysite.com/") != -1){
$("#announcer_box").hide();

I would like to show this element ID on my homepage,but exclude it from all other pages. how is this possible?

i am using wordpress.

On your home page, give the <html> or <body> tag a special class (like maybe "homepage"):

<html class='homepage'> <!-- plus any other classes you want/need -->

Then in your CSS:

#announcer_box { display: none; }
.homepage #announcer_box { display: block; }

This should work:

if ((window.location.protocol + "//" + window.location.hostname + "/") != (window.location.href)) {
        $("#announcer_box").hide();
 }

you might have a <body class="homepage"> , then, something like that ?

if($("body").hasClass("home")
  $("#announcer_box").hide();

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