2

我的网站上有这个 jQuery 代码

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

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

我正在使用wordpress。

4

3 回答 3

5

在您的主页上,给<html>or<body>标记一个特殊的类(例如“主页”):

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

然后在你的 CSS 中:

#announcer_box { display: none; }
.homepage #announcer_box { display: block; }
于 2013-11-04T16:11:02.053 回答
2

这应该有效:

if ((window.location.protocol + "//" + window.location.hostname + "/") != (window.location.href)) {
        $("#announcer_box").hide();
 }
于 2013-11-04T16:18:38.797 回答
0

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

if($("body").hasClass("home")
  $("#announcer_box").hide();
于 2013-11-04T16:11:11.990 回答