如何阻止 jQuery 匹配多个元素
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将上下文参数添加到 $() 函数。
$(".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.您可以通过不将类添加到链接来使此操作变得更加简单,因为您已经将类添加到父 td,因此可以使用 CSS 来定位链接:
Code before
Code
新 CSS 规则
这将起作用,因为“.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
Code after
New CSS rule
This will work because ".subMenuTDActive .subLink" has a higher specitivity than ".subLink", and will thus override the ".subLink" rule.
我和Drthomas在一起。 当你可以只使用 css 时,你到底为什么要使用 javascript 呢?
甚至更好(我认为),摆脱您不应该拥有且应该删除的 td 元素的样式,将链接设置为 display:block,然后您可以在一个元素下声明悬停样式:
或者 除非您无法控制 html,或者您有一些关键任务依赖于标记保持不变,否则“它已经在使用表格,我知道,起诉我”是一个非常糟糕的借口。 这是你的子菜单,没有表格:
大约花了 30 秒。 现在这是导航栏/滚动条的 CSS:
这可能又花了一分钟,因为我必须让它看起来很漂亮。
我并不是想欺负你或贬低你,但非语义代码不应该被鼓励,如果你正在寻求某事的帮助,我会觉得不合适,因为知道它正在发生在不合规的网站上使用。
I'm with drthomas. Why on earth are you using javascript at all when you can just use css?
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:
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:
That took about 30 seconds. Now here is the CSS for the navbar/rollover:
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.
您可以使用 AKX 的解决方案,或者:
You can use AKX's solution, or:
AKX的解决方案是正确的。 或者,您可以执行以下操作:
AKX's solution is correct. Alternatively, you can do: