【问题标题】:Make the clicked link color stays使点击的链接颜色保持不变
【发布时间】:2016-02-18 18:48:54
【问题描述】:

我一直在尝试使单击链接时,颜色停留在那里,不仅是在鼠标单击链接时(如果鼠标失去对链接的关注,颜色将恢复到正常状态)。

当你想问这个问题时,这个网站的颜色会停留在问问题标签上。

这是我尝试的 JSFiddle 上的代码示例:

HTML

<ul id="parentExample" style="display: block;">
  <asp:Repeater runat="server" ID="uiMenuList">
    <ItemTemplate>
      <li id="childExample">
        <a id="uiMenuText" href="http://www.google.com" target="_blank" runat="server">First Menu</a>
        <a id="uiMenuText" href="http://www.google.com" target="_blank" runat="server">Second Menu</a>
        <a id="uiMenuText" href="http://www.google.com" target="_blank" runat="server">Third Menu</a>
      </li>
    </ItemTemplate>
  </asp:Repeater>
</ul>

JavaScript

$(document.ready(function() {
  var $h3s = $('li').click(function() {
    $h3s.removeClass('active');
    $(this).addClass('active');
  });
}));

CSS

.active {
  background: none;
  color: #FFC000;
  border: 0px;
  margin-top: -3px;
  margin-right: auto;
  margin-left: auto;
}

#parentExample li#childExample a {
  background: none;
  color: black;
  border: 0px;
  margin-top: -3px;
  margin-right: auto;
  margin-left: auto;
}

#parentExample li#childExample a:hover {
  background: none;
  color: red;
  border: 0px;
  margin-top: -3px;
  margin-right: auto;
  margin-left: auto;
}

#parentExample li#childExample a:active {
  background: none;
  color: #FFC000;
  border: 0px;
  margin-top: -3px;
  margin-right: auto;
  margin-left: auto;
}

#parentExample li#childExample a:selected {
  background: none;
  color: #FFC000;
  border: 0px;
  margin-top: -3px;
  margin-right: auto;
  margin-left: auto;
}

以下是上述代码的示例: JS Fiddle

任何帮助将不胜感激

谢谢

【问题讨论】:

  • 您的设置不正确。您的代码应该是 $(function() { var $h3s = $('li').click(function() { console.log($h3s); $h3s.removeClass('active'); $(this).addClass('active'); }); }); 并将站点设置为加载 jQuery。

标签: javascript html css asp.net


【解决方案1】:

试试这个方法

$(document).ready(function() {
   $('li a').click(function() {
    $('li a').removeClass('active');
    $(this).addClass('active');
  });
});

改变你的 CSS 类位置。

喜欢

#parentExample li#childExample a {
  background: none;
  color: black;
  border: 0px;
  margin-top: -3px;
  margin-right: auto;
  margin-left: auto;
}

#parentExample li#childExample a:hover {
  background: none;
  color: red;
  border: 0px;
  margin-top: -3px;
  margin-right: auto;
  margin-left: auto;
}

#parentExample li#childExample a:active {
  background: none;
  color: #FFC000;
  border: 0px;
  margin-top: -3px;
  margin-right: auto;
  margin-left: auto;
}

#parentExample li#childExample a:selected {
  background: none;
  color: #FFC000;
  border: 0px;
  margin-top: -3px;
  margin-right: auto;
  margin-left: auto;
}

#parentExample li#childExample a.active {
      background: none;
      color: #FFC000;
      border: 0px;
      margin-top: -3px;
      margin-right: auto;
      margin-left: auto;
    }

https://jsfiddle.net/mzwwrsca/3/

【讨论】:

  • 嗨,即使我已经尝试过这个解决方案,它也不起作用,接受的答案对我有用。还是谢谢
  • 为什么要使用重要。
【解决方案2】:

你应该给!important,因为你想用class覆盖css而不是id
因为idspecificityclass

.active {
  background: none !important;
  color: #FFC000 !important;
  border: 0px !important;
  margin-top: -3px !important;
  margin-right: auto !important;
  margin-left: auto !important;
}

而且你不能使用$h3 这样的变量来制作点击事件。

你应该添加和删除类到锚标记。

如果你想使用变量,那么你可以像下面这样使用:

var h3s = $('li a');
   $('li a').click(function() {
    h3s.removeClass('active');
    $(this).addClass('active');
  });

别忘了包含 JQuery 插件。

Working Fiddle

【讨论】:

  • @ketan 您提到了特异性并提供了!important 作为解决方案,这似乎是错误的。 @Fuhans,您应该尝试为您的样式和更多类使用更少的 ID 选择器。正是因为它们不太具体且更容易覆盖。另外,将!important 添加到像.active 这样的泛型类中只会因为特殊性而使您的生活更加艰难。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-25
  • 2012-12-31
  • 2012-02-13
  • 1970-01-01
  • 2013-10-29
  • 2012-05-21
  • 2014-03-20
相关资源
最近更新 更多