【问题标题】:Structural Pseudo Classes and attribute selectors doesn't work together结构伪类和属性选择器不能一起工作
【发布时间】:2023-04-02 21:24:01
【问题描述】:

我有这个 HTML 代码:

<div class="field">                  
<input type="hidden" value="" name="a" id="a"> <input type="hidden" value="xxxx" name="b" id="b">
<input type="file" value="" name="file1"> <input type="file" value="" name="file2">
<input type="file" value="" name="file3"> <input type="file" value="" name="file4">
<input type="file" value="" name="file5"> <input type="file" value="" name="file6">
<input type="file" value="" name="file7"> <input type="file" value="" name="file8">     </div>

在这个 HTML 中,我想隐藏除第一个之外的 div class="field" 中的所有输入 type="file"。 我无法更改 HTML(添加类)。 我尝试同时应用伪类和结构化选择器来完成任务:

.field input[type='file']{
  display:none;
}

.field input[type='file']::first-child{
display:block;
}

但它似乎不起作用。 任何人都可以建议我在 css 中使用伪类和选择器的正确语法来解决这个问题?

【问题讨论】:

    标签: css css-selectors pseudo-class


    【解决方案1】:

    伪类只使用一个冒号,所以它是:first-child,而不是::first-child

    但是你的第一个input[type='file'] 不是第一个孩子,所以无论如何你都不能使用:first-child

    您必须切换规则并使用同级选择器来隐藏后续文件上传输入:

    .field input[type='file'] {
        display:block;
    }
    
    .field input[type='file'] ~ input[type='file'] {
        display:none;
    }
    

    此技术在here 中有进一步的描述,可用于大多数其他简单的选择器,而不仅仅是类和属性。

    【讨论】:

      【解决方案2】:

      您可以将此代码用于所有值,并且您将在 div class="field" 中隐藏所有输入 type="file",但第一个除外。试试这个代码。

      <html>
      <head>
      <style>
      .field input[type='file']
      {visibility:hidden;}
      </style>
      </head>
      <body>
      </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-02-15
        • 1970-01-01
        • 2014-02-03
        • 1970-01-01
        • 2015-08-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多