繁体   English   中英

下一个匹配兄弟姐妹的CSS3选择器

[英]CSS3 selector for next matching sibling

我正在尝试为以下HTML提供CSS3选择器:

<input data-type="fillpart" type="checkbox" name="cough, " checked="true">
<ignore></ignore>
<fillpart explicitlyset="true" name="cough, ">cough, </fillpart>
...
<input data-type="fillpart" type="checkbox" name=“fever, " checked="true">
<ignore></ignore>
<fillpart explicitlyset="true" name=“fever, ">fever, </fillpart>
...
<input data-type="fillpart" type="checkbox" name=“flu, " checked="true">
<ignore></ignore>
<fillpart explicitlyset="true" name=“flu, ">flue, </fillpart>

所有元素都是同级元素,并且...可能还有其他元素。 有问题的元素始终按三(3)的顺序排列。 目前,我的CSS如下:

fillpart {
  background: #a5d2a5;
}

它为<fillpart>的文本<fillpart> ,而标记本身显然被忽略,因为浏览器不支持。 我想根据先前的标记是否匹配input[checked='true']false来替换背景值。

CSS3有可能吗?

谢谢

要在选中的输入字段之后设置元素的样式,可以使用CSS的下一个选择器(+):

fillpart {
    background-color: #a5d2a5;
}

input:checked + ignore + fillpart {
    background-color: red;
}

这将使fillpartignore之后,在checked input-field具有红色背景色之后

编辑:

实际上,您可以使用~ selector,它选择每个以选中的输入字段为fillpart元素:

fillpart {
    background-color: #a5d2a5;
}

input:checked ~ fillpart {
    background-color: red;
}

您可以在选择器中使用加号(+):

input[checked="false"] + ignore + fillpart

查看下面的示例:

 fillpart {background: #a5d2a5;} input[checked="false"] +ignore+ fillpart{background: red;} 
 <!DOCTYPE html> <html> <body> <input data-type="fillpart" type="checkbox" name="cough, " checked="true"> <ignore></ignore> <fillpart explicitlyset="true" name="cough, ">cough, </fillpart> ... <input data-type="fillpart" type="checkbox" name=“fever, " checked="false"> <ignore></ignore> <fillpart explicitlyset="true" name=“fever, ">fever, </fillpart> ... <input data-type="fillpart" type="checkbox" name=“flu, " checked="true"> <ignore></ignore> <fillpart explicitlyset="true" name=“flu, ">flue, </fillpart> </body> </html> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM