如何选择不在特定选择器中的:hover
我想对不在特定 id 中的 a
元素进行样式设置。例如,假设这是我的 HTML:
<div id="boat">
<a href="#">I'm in a boat! ☻</a>
</div>
<a href="#">☹</a>
现在,假设我只想设置不在船上的 a
元素的样式。我期望类似 a:hover:not(#boat a)
的东西能够工作,但可惜的是,它没有。有什么办法可以让我做我想做的事吗?
I would like to style a
elements that are not in a certain id. For example, let's say this is my HTML:
<div id="boat">
<a href="#">I'm in a boat! ☻</a>
</div>
<a href="#">☹</a>
Now, let's say I only want to style the a
element that's not in a boat. I expected something similar to a:hover:not(#boat a)
to work, but alas, it doesn't. Is there a way I can do what I want to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当谈到 jQuery 选择器时,
a:hover
将选择所有应用了 CSS 伪类:hover
的a
元素(即 < code>a 当前鼠标悬停在其上的元素)。这是选择过程,而不是样式选择,要选择您正在讨论的元素,请尝试以下操作:
DEMO: http://jsfiddle.net/marcuswhybrow/AMWhU/
如果悬停 jQuery 效果是在您做出选择后,只需绑定即可活动如常:
When it comes to jQuery selectors
a:hover
will select all thea
elements which have the CSS pseudo class:hover
applied to it (i.e.a
elements which are currently being hovered over with the mouse).This is the selection process, and not the styling selection, to select the elements you are talking about, try this instead:
DEMO: http://jsfiddle.net/marcuswhybrow/AMWhU/
If a hover jQuery effect is waht you are after the once you have made the selection just bind the event as usual:
不在该
div#id
内的链接将具有.closest('div#id').length === 0
,并且由于 0 为 false,因此您可以在与以下相反的方式设置这些链接的样式:这是一个示例。
Links not within that
div#id
will have.closest('div#id').length === 0
, and since 0 is false, you can select on the opposite of those as follows to style those links:Here's an example.
看起来您可以使用否定属性选择器('!=')。给定(大部分)您的原始 html:
在 javascript 控制台,运行:
查找所有没有 id 'boat' 且带有锚子节点的节点。
It looks like you can use a negating attribute selector ('!='). Given (mostly) your original html:
At the javascript console, running:
Looks for all nodes that do not have the id 'boat', with an anchor child.