CSS 选择器反转选择

标签 css selector

在反向选择器逻辑上使用 :not() 有什么优点或缺点吗?可能是在性能、安全性或浏览器支持方面,推荐哪种方法?

或者:

.imageSlider img:not(:first-child) {
  display: none;
}

或者:

.imageSlider img {
  display: none;
}

.imageSlider img:first-child {
  display: block;
}

最佳答案

有时使用 :not 会更好。

<p class="my-paragraph">
    <div class="something"></div>
    <div class="something-else"></div>
    <div class="an-other-thing"></div>
    <div class="an-other-thing"></div>
    <div class="last-one"></div>
</p>

在这种情况下,如果您想隐藏所有内容,除了 div .an-other-thing 这样写会更快:

.my-paragraph div:not(.an-other-thing) {
    display: none;
}

代替:

.my-paragraph div {
    display: none;
}

.my-paragraph div.an-other-thing {
    display: block;
}

在大多数情况下,更长的 CSS 意味着更长的执行时间

关于CSS 选择器反转选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31021902/

相关文章:

Javascript 文件修改不是从中调用的 html 文件? Jquery 选择器

jquery - 这在 jQuery 中合法吗?

redux - react native中的mapStateToProps、mapDispatchToProps类型和选择器有什么区别

html - 在某些屏幕尺寸下,部分侧边栏低于内容

html - 基于 CSS 的网站无法在 Internet Explorer 上运行

javascript - 如何使用 bootstrap 在所有 4 个选项卡上复制相同的导航菜单?

objective-c - 检查 ObjC 选择器 (SEL) 是否有参数

html - 如何在移动设备上堆叠李的垂直但在桌面上保持水平

html - 试图使按钮向右浮动,但不起作用

javascript - 需要使用兄弟选择器切换/删除类