更改所选菜单选项卡的颜色

发布于 2024-09-24 05:13:42 字数 1162 浏览 3 评论 0原文

我从另一个问题中获取了这个片段:

<script type='text/javascript' >
$(document).ready(function () {
 $("div.content ul li a")
 .mouseover(function () {
  var t = $(this);
  if (!t.hasClass("clicked")) {  // very easy to check if element has a set of styles
   t.addClass('mouseover');
  }
 })
 .mouseout(function () {  // attach event here instead of inside mouse over
  $(this).removeClass('mouseover');
 });

 $("div.content ul li a").click(function () {
  var t = $(this);
  t.toggleClass("clicked");
  if (t.hasClass("clicked")) {
   t.removeClass('mouseover');
  } else {
   t.addClass('mouseover');
  }
 });
});
</script>

我想要的最后一件事是在单击另一个选项卡时恢复选项卡正常的 css。 例如,当我单击 tab1 时,选项卡的背景颜色为白色,当我进入 Tab2 时,选项卡的背景颜色变为黑色。Tab1 变为白色,Tab2 变为黑色,

<ul> 
 <li>
  <a href="#Tab1">Tab 1</a>
 </li>
 <li>
  <a href="#Tab2">Tab 2</a>
 </li>
</ul> 

假设这是 CSS 部分

ul li a {background-color: white;}
ul li a.mouseover {background-color: black;}
ul li a.mouseout {background-olor: white;}
ul li a.clicked {background-color: black;}

I grabbed this snippet from another question:

<script type='text/javascript' >
$(document).ready(function () {
 $("div.content ul li a")
 .mouseover(function () {
  var t = $(this);
  if (!t.hasClass("clicked")) {  // very easy to check if element has a set of styles
   t.addClass('mouseover');
  }
 })
 .mouseout(function () {  // attach event here instead of inside mouse over
  $(this).removeClass('mouseover');
 });

 $("div.content ul li a").click(function () {
  var t = $(this);
  t.toggleClass("clicked");
  if (t.hasClass("clicked")) {
   t.removeClass('mouseover');
  } else {
   t.addClass('mouseover');
  }
 });
});
</script>

The last thing I wanted is to restore the tabs normal css when another tab is clicked.
For example, the tab's bgcolors are white when I click tab1 it becomes black when I go into Tab2..Tab1 goes white and Tab2 goes black

<ul> 
 <li>
  <a href="#Tab1">Tab 1</a>
 </li>
 <li>
  <a href="#Tab2">Tab 2</a>
 </li>
</ul> 

let's say here's the CSS part

ul li a {background-color: white;}
ul li a.mouseover {background-color: black;}
ul li a.mouseout {background-olor: white;}
ul li a.clicked {background-color: black;}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

森林迷了鹿 2024-10-01 05:13:42

实际上,您可以为此大大简化 Javascript。这应该会达到你想要的效果。

<script type="text/javascript">
    $(document).ready(function() {
        $("div.content ul li a")
         .mouseover(function() {
             $(this).addClass('mouseover');
         })
         .mouseout(function() {
             $(this).removeClass('mouseover');
         });

        $("div.content ul li a").click(function(e) {
            e.preventDefault(); //prevent the link from actually navigating somewhere
            $(this).toggleClass("clicked");
            $("div.content ul li a").not(this).removeClass("clicked"); //remove the clicked class from all other elements
        });
    });
</script>

这里的 Javascript 将执行以下操作:

  • 当您悬停链接时添加“mouseover”类
  • 当您不再悬停链接时删除“mouseover”类
  • 当您单击链接时,它将切换“clicked”类并将其从任何其他可能具有该类的链接 - 这会将您的其他选项卡恢复到“正常”状态。

You can actually greatly simplify your Javascript for this. This should achieve your desired effect.

<script type="text/javascript">
    $(document).ready(function() {
        $("div.content ul li a")
         .mouseover(function() {
             $(this).addClass('mouseover');
         })
         .mouseout(function() {
             $(this).removeClass('mouseover');
         });

        $("div.content ul li a").click(function(e) {
            e.preventDefault(); //prevent the link from actually navigating somewhere
            $(this).toggleClass("clicked");
            $("div.content ul li a").not(this).removeClass("clicked"); //remove the clicked class from all other elements
        });
    });
</script>

The Javascript here will do the following:

  • Add the "mouseover" class when you hover a link
  • Remove the "mouseover" class when you no longer hover a link
  • When you click a link, it will toggle the "clicked" class and remove it from any other link that may have had the class - this will restore your other tabs to their "normal" state.
樱娆 2024-10-01 05:13:42

@wsanville

双击同一个选项卡的问题怎么样?

如果我在单击选项卡时从选项卡(指示选定的选项卡)中删除底部边框,那就没问题了。但是当我再次单击它时,它应该保持这样(没有底部边框),但由于现在切换,看起来您尚未选择该选项卡,但您仍然在那里。

@wsanville

What about the problem of double-clicking the same tab?

If i remove a bottom-border from a tab (indicating the selected one) when clicked on a tab, that's fine. But when i click it again, it should just stay like that (no bottom border), but because of the toggle now it looks like you haven't selected the tab but you are still there.

成熟稳重的好男人 2024-10-01 05:13:42

仅使用 CSS 即可实现您想要的效果:

ul li a {background-color: white;}
ul li a:hover {background-color: black;}
ul li a:focus {background-color: black;}

演示

It is possible to achieve what you are looking for using just CSS:

ul li a {background-color: white;}
ul li a:hover {background-color: black;}
ul li a:focus {background-color: black;}

Demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文