jQuery仅在主页上显示元素ID

jQuery show element ID only on homepage

本文关键字:显示 元素 ID 主页 jQuery      更新时间:2023-09-26

我的网站上有这个jQuery代码

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

我想在我的主页上显示此元素 ID,但将其从所有其他页面中排除。 这怎么可能?

我正在使用WordPress。

在您的主页上,为<html><body>标记一个特殊的类(例如"主页"):

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

然后在你的 CSS 中:

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

这应该有效:

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

你可能有一个<body class="homepage">,那么,类似的东西?

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