【问题标题】:Doubt in css selector > [#id (.class1 or .class2)]css 选择器中的疑问 > [#id (.class1 or .class2)]
【发布时间】:2010-09-29 02:37:38
【问题描述】:

我认为应该存在更好的方法来做到这一点......

我正在使用跟随 CSS 选择器

#book_form .ano_chegada, #book_form .ano_partida {...}

HTML:

<form id="book_form">
<input class='ano_chegada' .../>
<input class='ano_partida' .../>
</form>

我真的不喜欢重复表单 id 两次。是绝对必要的吗? 我知道它应该在没有第二个 id (#book_form) 的情况下工作,但它会选择共享同一类 (.ano_partida) 的所有元素,对吧?

谢谢 =)(Obrigado,葡萄牙语)

来自葡萄牙的欢呼

【问题讨论】:

    标签: html css stylesheet css-selectors


    【解决方案1】:

    是的,如果您只想选择那些位于#book_form 中的类的元素,那么这是绝对必要的。

    CSS 本身不具有嵌套选择器,但是一些服务器端解决方案可能会为您提供 CSS 扩展,从而为您提供此类功能。

    【讨论】:

      【解决方案2】:

      只有一家浏览器供应商在 4 个月前在仍处于测试阶段的浏览器中实现了此功能。 见http://hacks.mozilla.org/2010/05/moz-any-selector-grouping/

      【讨论】:

        【解决方案3】:

        您可以稍微调整一下 HTML 以使 CSS 选择器更易于使用。 (这不一定值得,并且可能会增加输出的大小。)

        在类标签中添加另一个类名:

        <form id="book_form"> 
        <input class='ano_chegada something' .../> 
        <input class='ano_partida something' .../> 
        </form>
        
        #book_form .something {...}
        

        或者把现有的东西分成多个类:

        <form id="book_form"> 
        <input class='ano chegada' .../> 
        <input class='ano partida' .../> 
        </form>
        
        #book_form .ano {...}
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-01-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-03-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多