如果元素具有特定类,则不要应用 css 样式
我有一个有序列表:
<ol id="selectable">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ol>
当我将鼠标悬停在某个项目上时,我希望它具有某种突出显示,因此我有:
#selectable li:hover
{
/* some style */
}
使用 JavaScript,我为这些列表项提供了单击时会更改样式的属性。例如,如果我单击第 4 项,它将具有红色背景颜色。
现在,如果元素恰好具有 ckicked 样式(红色背景),如何禁用悬停样式?
换句话说,我不想:
#selectable li:hover
{
/* some style */
}
应用于所选的 li...我该怎么做?
I have an ordered list:
<ol id="selectable">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ol>
when I hover over an item I want it to have some sort of highlight therefore I have:
#selectable li:hover
{
/* some style */
}
with JavaScript I gave those list items the attribute that when clicked they change the style. so if I click on Item 4 for example it will have a red background color.
now how can I disable the hover style if the element happens to be with the style ckicked (red background)?
In other words I don't want :
#selectable li:hover
{
/* some style */
}
to apply on li that are selected... how can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
CSS3:
}
HTML:
CSS3:
}
HTML:
您需要覆盖
hover
样式You will need to override the
hover
style真正的 CSS3 答案是
:not()
选择器。您可以将现有的 CSS 更改为以下内容:
然而不幸的是,这个答案可能不是您想要的,因为 IE8 及更早版本不支持
:not()
选择器。您可以使用Selectivizr之类的工具来向 IE 添加对此的支持,但坦率地说,这很难对于这个例子来说这是值得的,因为您可以轻松添加另一种 CSS 样式来覆盖另一种样式:
希望有帮助。
The real CSS3 answer to this is the
:not()
selector.You'd change your existing CSS to the following:
Unfortunately however, this answer is probably not the one you want, because IE8 and earlier don't support the
:not()
selector.You could use a tool like Selectivizr to add support for this to IE, but frankly, it's hardly worth it for this example, as you can easily add another CSS style to override the other one:
Hope that helps.