如何阻止 jQuery 匹配多个元素

发布于 2024-07-29 11:16:10 字数 1133 浏览 3 评论 0原文

我正在尝试使用 jQUery 创建翻转效果。 我有类似的事情,但是因为我试图对其中有链接的对象进行鼠标悬停,所以我遇到了问题。

我有一个 2 级导航栏。 占位符是表格单元格(我知道不好 - 但它已经这样做了)。 我想将背景从白色 (#FFF) 更改为深灰色 (#999)。 我还想将文本从深灰色更改为白色。

由于文本是一个链接,我必须在链接标记中指定一个类,以确保它是没有下划线的深灰色,并且不会默认为蓝色带下划线的文本。

我编写的代码会导致 class="subLink" 的所有链接在其中任何一个“悬停”时从灰色变为白色。 我只希望这种情况发生在有问题的特定项目上 - 即背景应该变成灰色,链接应该变成白色。

HTML 和 jQuery 如下:-

<td class="subMenuTD"><a href="newsletter.html" class="subLink">Newsletter</a></td>
<td class="subMenuTD"><a href="news_archive.html" class="subLink">News Archive</a></td>
<td class="subMenuTD"><a href="events.html">Events</a></td>

$(".subMenuTD").hover(
                  function() { 
                     $(this).addClass("subMenuTDActive");
                     $(".subLink").addClass("subLink-white");
                  },
                  function() {      
                     $(this).removeClass("subMenuTDActive");
                     $(".subLink").removeClass("subLink-white");
          }
               );

I am trying to create a rollover effect with jQUery. I have similar things, however because I am trying to do a mouseover with a an object that has a link in it I am having problems.

I have a level-2 nav bar. The placeholder's are table cells (bad I know - but it's already done this way). I want to change the background from white (#FFF) to dark grey (#999). I also want to change the text from dark grey to white.

Since the text is a link, I have to specify a class within the link tag to ensure that it is dark grey without an underline and doesn't default to the blue, underlined text.

The code I have written causes all the links of class="subLink" to change to being white from grey when anyone of them is "hovered over". I only want this to happen for the particular item in question - that is the background should become grey and the link should become white.

HTML and jQuery are below:-

<td class="subMenuTD"><a href="newsletter.html" class="subLink">Newsletter</a></td>
<td class="subMenuTD"><a href="news_archive.html" class="subLink">News Archive</a></td>
<td class="subMenuTD"><a href="events.html">Events</a></td>

$(".subMenuTD").hover(
                  function() { 
                     $(this).addClass("subMenuTDActive");
                     $(".subLink").addClass("subLink-white");
                  },
                  function() {      
                     $(this).removeClass("subMenuTDActive");
                     $(".subLink").removeClass("subLink-white");
          }
               );

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

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

发布评论

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

评论(5

甜味超标? 2024-08-05 11:16:10

将上下文参数添加到 $() 函数。

$(".subLink") --> $(".subLink", this) 将使 jQuery 只匹配给定上下文元素的子元素“.subLink”。

Add a context parameter to the $() function.

$(".subLink") --> $(".subLink", this) will make jQuery only match ".subLink"s that are children of the given context element.

谎言 2024-08-05 11:16:10

您可以通过不将类添加到链接来使此操作变得更加简单,因为您已经将类添加到父 td,因此可以使用 CSS 来定位链接:

Code before

$(".subMenuTD").hover(                       
  function() {
     $(this).addClass("subMenuTDActive");                                
     $(".subLink").addClass("subLink-white");
  },
  function() {
     $(this).removeClass("subMenuTDActive");                                
     $(".subLink").removeClass("subLink-white");
  }
);

Code

$(".subMenuTD").hover(                       
  function() {
     $(this).addClass("subMenuTDActive");     
  },
  function() {
     $(this).removeClass("subMenuTDActive");    
  }
);

新 CSS 规则

.subMenuTDActive .subLink {
   /* Add hover link styling here */
}

这将起作用,因为“.subMenuTDActive .subLink”比“.subLink”具有更高的特异性,因此将覆盖“.subLink”规则。

You can make this even simpler by not adding the class to the link, since you are already adding a class to the parent td, and can thus target the link by using CSS:

Code before

$(".subMenuTD").hover(                       
  function() {
     $(this).addClass("subMenuTDActive");                                
     $(".subLink").addClass("subLink-white");
  },
  function() {
     $(this).removeClass("subMenuTDActive");                                
     $(".subLink").removeClass("subLink-white");
  }
);

Code after

$(".subMenuTD").hover(                       
  function() {
     $(this).addClass("subMenuTDActive");     
  },
  function() {
     $(this).removeClass("subMenuTDActive");    
  }
);

New CSS rule

.subMenuTDActive .subLink {
   /* Add hover link styling here */
}

This will work because ".subMenuTDActive .subLink" has a higher specitivity than ".subLink", and will thus override the ".subLink" rule.

一个人练习一个人 2024-08-05 11:16:10

我和Drthomas在一起。 当你可以只使用 css 时,你到底为什么要使用 javascript 呢?

 .subMenuTD:hover {
                   background-color: #ccc;
                   }

 .subLink:hover {
                 color: #fff;
                 }

甚至更好(我认为),摆脱您不应该拥有且应该删除的 td 元素的样式,将链接设置为 display:block,然后您可以在一个元素下声明悬停样式:

 .subLink {
          display: block;
          }

  .subLink:hover {
                 background-color: #ccc;
                 color: #fff;
                 }

或者 除非您无法控制 html,或者您有一些关键任务依赖于标记保持不变,否则“它已经在使用表格,我知道,起诉我”是一个非常糟糕的借口。 这是你的子菜单,没有表格:

<ul class="subMenu">
<li><a href="newsletter.html">Newsletter</a></li>
<li><a href="news_archive.html">News Archive</a></li>
<li><a href="events.html">Events</a></li>
</ul>

大约花了 30 秒。 现在这是导航栏/滚动条的 CSS:

 .subMenu li {
          display: inline;
          list-style: none;
          }

  .subMenu a {
          color: #000;
          background-color: #fff;
          text-decoration: none;
          display: block;
          }

   .subMenu a:hover {
          color: #fff;
          background-color: #ccc;
          }

这可能又花了一分钟,因为我必须让它看起来很漂亮。

我并不是想欺负你或贬低你,但非语义代码不应该被鼓励,如果你正在寻求某事的帮助,我会觉得不合适,因为知道它正在发生在不合规的网站上使用。

I'm with drthomas. Why on earth are you using javascript at all when you can just use css?

 .subMenuTD:hover {
                   background-color: #ccc;
                   }

 .subLink:hover {
                 color: #fff;
                 }

Or even better (me thinks), get rid of styling the td elements that you shouldn't have and ought to be working on removing, set the links as display:block, and then you can declare the hover style under one element:

 .subLink {
          display: block;
          }

  .subLink:hover {
                 background-color: #ccc;
                 color: #fff;
                 }

And unless you have no control over the html or you have something mission-critical that relies on the markup staying the same, "it's already using tables, i know, sue me" is a really poor excuse. Here is your sub-menu, sans table:

<ul class="subMenu">
<li><a href="newsletter.html">Newsletter</a></li>
<li><a href="news_archive.html">News Archive</a></li>
<li><a href="events.html">Events</a></li>
</ul>

That took about 30 seconds. Now here is the CSS for the navbar/rollover:

 .subMenu li {
          display: inline;
          list-style: none;
          }

  .subMenu a {
          color: #000;
          background-color: #fff;
          text-decoration: none;
          display: block;
          }

   .subMenu a:hover {
          color: #fff;
          background-color: #ccc;
          }

That took maybe a minute more, because I had to make it look pretty.

I'm not trying to be a bully or come down on you, but non-semantic code is not something that should be encouraged, and if you are looking for help with something, I wouldn't feel right giving it knowing it was going to be used on a site that wasn't compliant.

暖伴 2024-08-05 11:16:10

您可以使用 AKX 的解决方案,或者:

$(".subMenuTD").hover(
                    function() { 
                            $(this)
                               .addClass("subMenuTDActive")
                               .children(".subLink")
                               .addClass("subLink-white");
                    },
                    function() {            
                            $(this)
                                .removeClass("subMenuTDActive");
                                .children(".subLink")
                                .removeClass("subLink-white");
                    }
            );

You can use AKX's solution, or:

$(".subMenuTD").hover(
                    function() { 
                            $(this)
                               .addClass("subMenuTDActive")
                               .children(".subLink")
                               .addClass("subLink-white");
                    },
                    function() {            
                            $(this)
                                .removeClass("subMenuTDActive");
                                .children(".subLink")
                                .removeClass("subLink-white");
                    }
            );
请别遗忘我 2024-08-05 11:16:10

AKX的解决方案是正确的。 或者,您可以执行以下操作:

$(this).children(".subLink")

AKX's solution is correct. Alternatively, you can do:

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