【问题标题】:Simple not selector not working in CSS [duplicate]简单的非选择器在 CSS 中不起作用 [重复]
【发布时间】:2017-02-22 16:36:09
【问题描述】:

我有一个简单的例子来解释 CSS 中的 not 选择器。我希望每个元素的边框都是红色的。但没有人拥有。问题出在哪里?

[name!='er']{border:2px solid red;}
<p name='peter'>This is paragraph 1.</p>

<p name='julie'>This is paragraph 2.</p>

<p name='jennifer'>This is paragraph 3.</p>

<p name='george'>This is paragraph 4.</p>

<p name='gilbert'>This is paragraph 6.</p>

<span name='rosy'>This is span 1.</span>

<span name='robert'>This is span 2.</span>

https://api.jquery.com/attribute-not-equal-selector/

这个选择器存在于 jQuery 中。我也希望 CSS 也一样。不是吗?

【问题讨论】:

  • 每个元素?你期望你的选择器到底做什么? css中没有这样的选择器(!=)
  • name!="er" 不是 not 选择器
  • "这个选择器存在于 jQuery 中。我希望 CSS 也一样。不是吗?"我不知道,链接告诉你什么?

标签: css css-selectors


【解决方案1】:

您是否尝试过使用:not 选择器?

:not([name='er']) { 
  border: 2px solid red;
}

【讨论】:

    【解决方案2】:

    这就是你的选择器应该的样子。

    :not([name='er'])

    您需要了解:not 运算符。

    *:not([name='er']) {
      border: 2px solid red;
    }
    <p name='peter'>This is paragraph 1.</p>
    
    <p name='julie'>This is paragraph 2.</p>
    
    <p name='jennifer'>This is paragraph 3.</p>
    
    <p name='george'>This is paragraph 4.</p>
    
    <p name='gilbert'>This is paragraph 6.</p>
    
    <span name='rosy'>This is span 1.</span>
    
    <span name='robert'>This is span 2.</span>

    【讨论】:

    • "您需要了解 :not 运算符。"你可能也需要刷一下。那两个 * 不应该在那里。第一个 * 和 :not() 之间的空格很重要。当然,第二个 * 在某些浏览器中有效,但没有任何理由将它放在 :not() 中,而且 :not() 中的复合选择器并非在所有浏览器中都有效。
    • @BoltClock 谢谢。我刚刚更新了你说的两件事。我也希望所有现代浏览器都支持:not 运算符。
    猜你喜欢
    • 2014-05-23
    • 1970-01-01
    • 2014-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    • 2019-01-08
    • 2014-03-29
    相关资源
    最近更新 更多