排除logo部分的访问/悬停效果在jquery



我有一个用于导航的静态代码,

<div class="nav__show-hide--800 step-asia__grid">
  <div class="step-asia__grid-12--col-5">
    <div class="nav__left">
      <a href="http://localhost/step-asia/about.php">About Us</a>
      <a href="http://localhost/step-asia/whatwedo.php">What We Do</a>
    </div>
  </div>
  <div class="step-asia__grid-12--col-2">
    <div class="nav__logo nav__logo--height">
      <a href="http://localhost/step-asia/" id="logo"><img src="assets/image/logo-1.png" alt="Step Asia Logo"></a>
    </div>
  </div>
  <div class="step-asia__grid-12--col-5">
    <div class="nav__right">
      <a href="http://localhost/step-asia/how-it-works.php">How It Works</a>
      <a href="http://localhost/step-asia/contact.php">Contact Us</a>
    </div>
  </div>
</div>

和确定当前页面的jquery,并将添加一个活动类,使下划线作为悬停效果,以指示用户访问的当前页面

$(document).ready(function() {
  $("[href]").each(function() {
    if (this.href == window.location.href) {
      if (! $('#logo')) {
        $(this).addClass("active");         
      }
    }
  });
});

但是我想排除这部分

<a href="http://localhost/step-asia/" id="logo"><img src="assets/image/logo-1.png" alt="Step Asia Logo"></a>

有更好的主意吗?

使用.not()从选择器中排除特定元素

$("[href]").not("#logo").each(function() {
    if ($(this).attr("href") === window.location.href) {
        $(this).addClass("active");         
    }
});
如果: 可以直接进入而不是.not():not()
if (this.id !== "logo" && this.getAttribute("href") === window.location.href) {

这可以使用简单的css完成。在你的例子中:

.active #logo:hover{
  text-decoration:none !important;
}

您将类活动添加到徽标,但这将覆盖它。

最新更新