【问题标题】:Making Active Page styling different on certain page with jquery/ghost使用 jquery/ghost 使特定页面上的活动页面样式不同
【发布时间】:2015-09-22 09:25:12
【问题描述】:

http://i.stack.imgur.com/Q9OMB.png

这是我的一个小问题。基本上我正在为 Ghost CMS 做一个主题,但我遇到了一个困扰我几个小时的问题,我自己无法解决这个问题,也没有从谷歌找到同类问题的来源/sof 两者都没有。

我的目标是使用 jquery 制作每个页面具有不同样式的活动页面样式(主页为红色,关于蓝色等),因为我无法在 Ghost 本身中完成它,因为它想要旋转具有相同样式的单个循环所有链接。

到目前为止的 Jquery 代码

$(function(){  
  $('a').each(function() {
      if ($(this).prop('href') == window.location.href) {
      $(this).addClass('homeCurrent');}
      });  
});

导航栏的相关HTML

<ul class= "nav navbar-nav navbar-left">
<li id="home" class="home hvr-sweep-to-top-home "><a href="/">Home</a></li>
<li id="about" class="about hvr-sweep-to-top-about"><a href="/page-about/">About</a></li>
</ul>

我尝试使用 jquery 运行不同类型的 IF 语句,但没有成功。

代码的逻辑如下:

if page is home = li style is homeCurrent

<ul class= "nav navbar-nav navbar-left">
<li id="home" class="home hvr-sweep-to-top-home homeCurrent"><a href="/">Home</a></li>
<li id="about" class="about hvr-sweep-to-top-about "><a href="/page-about/">About</a></li>
</ul>

if page is about = li style is aboutCurrent

<ul class= "nav navbar-nav navbar-left">
<li id="home" class="home hvr-sweep-to-top-home"><a href="/">Home</a></li>
<li id="about" class="about hvr-sweep-to-top-about aboutCurrent"><a href="/page-about/">About</a></li>
</ul>

有人吗?

希望我包括了所有相关的内容。

【问题讨论】:

  • 嗯,你要为所有链接添加相同的类? -edit- 我看到你在问题中提到了这一点。
  • 当时一种风格,但需要在特定页面上有所不同。

标签: javascript jquery css ghost


【解决方案1】:

老实说,我会避免使用 window.location.href 比较,因为它只会让我觉得可能会出现问题。

我个人会做的是添加到关于页面的内容:

$(document).ready(function () {
   $('.about').addClass('aboutCurrent');
});

您将该代码添加到您希望添加 xxxCurrent 类的每个页面。

这避免了您必须像上面那样动态地执行它,这将根据 url 和浏览器遇到问题

【讨论】:

  • 感谢您的回答卡尔。我之前曾想过在 CSS 中手动执行此操作,但 Ghost 的结构使其不可能/非常困难。这是因为 ghost 有这个“default.hbs”页面,它具有基本的静态布局,没有每个页面的动态元素,这包括菜单/页脚 n 的东西。 themes.ghost.org/v0.6.4/docs/structure
  • @TonyA 但是你的“关于”页面的内容应该只对关于页面是唯一的。如果您将其添加到 about 页面的内容中,它只会在那里执行此操作。
  • @TonyA 如果你依赖window.location.href 那么任何偏差,即about.html?post=true 将意味着about 将不再获得类
  • @CarlK 我同意你的观点,但也许 OP 无法修改结构。
  • @Mikey 结构?
【解决方案2】:

尝试使用 switch 语句:

$(function () {
    $('a').each(function () {
        var link = $(this).prop('href');
        if (link == window.location.href) {

            switch (link) {
                case "home":
                    $(this).addClass('homeCurrent');
                    break;
                case "about":
                    $(this).addClass('aboutCurrent');
                    break;
                default:
                    $(this).addClass('nothingactive');
            }
        }
    });
});

编辑*

如果你真的想坚持使用这种检查 URL 的方法,你可以这样做:

$(function () {
    $('a').each(function () {
        var base = "http://www.yourdomain.com/";
        var link = base + $(this).prop('href');
        if (link == window.location.href) {

            switch (link) {
                case "http://www.yourdomain.com/home" :
                    $(this).addClass('homeCurrent');
                    break;
                case "http://www.yourdomain.com/about" :
                    $(this).addClass('aboutCurrent');
                    break;
                default:
                    $(this).addClass('nothingActive');
            }
        }
    });
});

【讨论】:

  • 一段精彩的代码!0 根本没有考虑 switch 语句。这在理论上应该可行,但由于某种原因,它现在一直在导出 class="nothingactive"。
  • 那是因为它失败了。 window.location.href 将返回您的 URL,例如:window.location.href = 'http://www.google.com'; 所以它可能不匹配任何大小写并运行默认值。顺便说一下,这只是一个示例,您应该根据需要对其进行修改。
猜你喜欢
  • 1970-01-01
  • 2016-12-27
  • 2013-06-04
  • 2010-09-09
  • 1970-01-01
  • 1970-01-01
  • 2017-04-06
  • 1970-01-01
  • 2014-03-11
相关资源
最近更新 更多