如何改变
我了解如何更改活动
元素的描述li:active {
...declarations...
}
,但是如何更改所有其他不活动元素?
例如,我的所有元素都是粗体,但是当我选择其中一个元素时,所有其他元素都会恢复正常。
谢谢!
I understand how to change the description of an active <li>
element
li:active {
...declarations...
}
But how can I change all the other elements that are NOT active?
For example, all my elements are in bold, but when I select one of them, all the others are changed back to normal.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我想 li:not(:active) 至少在理论上应该有效。
I'd imagine
li:not(:active)
should at least theoretically work.将规则应用于所有规则,然后将不同的规则应用于活动规则。
结果:未激活的为蓝色。
Apply a rule to ALL of them, then apply a different rule to the active.
Result: the un-active ones are blue.
重读你的问题后,我认为真正的答案是你不能单独使用 CSS 来控制元素在用户交互中的行为方式。
我意识到这不起作用,因为样式会立即应用,并且 DOM 中的元素通常不是
:active
默认情况下:加上,
:not()
是一个 CSS3 伪类,因此如果您必须考虑较旧的浏览器,那么现在对它的支持相当差。也许你可以用 JavaScript 来做到这一点(我在这里使用 jQuery)...
After rereading your question, I think the real answer is that you can't use CSS alone to control how the elements behave on user interaction.
I realize that this won't work because the styles are applied immediately, and elements in the DOM are typically not
:active
by default:Plus,
:not()
is a CSS3 pseudo-class, so support for it is rather poor right now if you have to account for older browsers.Maybe you can do this with JavaScript (I use jQuery here)...
如果我理解正确的话,这应该可以做到,
所以基本上你想要在父级上有一个活动状态,它将所有内容切换到正常状态,然后为也处于活动状态的 li 覆盖它。
If I understand correctly this should do it,
So basically you want an active state on the parent which switches everything to normal and then override that for the li that is also active.
根据您的示例扩展 Brad 的答案:
您希望所有
均为粗体,直到单击一个,对吗?首先:
然后,如果单击一个列表项,则将该列表项保持为粗体,但使其他项保持常规:
~ 选择与活动 li 相同的所有元素,而不选择活动元素本身。
To expand Brad's answer based on your example:
You want all
<li>
's to be bold, until one is clicked, right? Start off with:Then, if a list item is clicked keep that one bold but make the others regular:
The ~ selects all elements that are siblings of the active li, without selecting the active one itself.