结构伪类和属性选择器不能一起工作

2024-03-29

我有这个 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 中使用伪类和选择器的正确语法来解决这个问题?


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

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

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

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

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

该技术进一步描述here https://stackoverflow.com/questions/2717480/css-selector-for-first-element-with-class/8539107#8539107,并且可用于大多数其他简单选择器,而不仅仅是类和属性。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

结构伪类和属性选择器不能一起工作 的相关文章

随机推荐