简体   繁体   English

“相邻”元素的CSS选择器

[英]CSS selector for “adjacent” elements

Need some help with CSS selectors as I am not sure how to express this in CSS, yet. 需要一些关于CSS选择器的帮助,因为我不确定如何在CSS中表达它。 How would I get the last span "Error" to show up when an :invalid pseudo-class appears on the input element (and of course be hidden when no :invalid is present)? input元素上出现:invalid伪类时,如何显示最后一个跨度“错误”(当然,当不存在:invalid时,隐藏该类)? No Javascript, only CSS please. 没有Javascript,只有CSS请。

<div class="row-fluid">
    <div class="control-group control-field-nickname">
        <span class="span3 offset1">
            <label>{{ msgs.mid("nickname") }}</label>
        </span>
        <span class="span3 input-prepend">                              
            <i class="add-on icon-user"></i>
            <input class="form-field" type="email" id="field-nickname">
        </span>
        <span class="span1">Error!</span>
    </div>
</div>

You can't. 你不能。

CSS has no selector / combinator that expresses "contains" so you cannot say "A span adjacent to a span which contains an input that is invalid". CSS没有表达“包含”的选择器/组合器,因此您不能说“与包含无效输入的范围相邻的范围”。

The proposed subject feature of Selectors Level 4 will allow you to select elements other than the last one that appears in the selector, but (AFAIK) not let you go up and then down. 建议使用的选择器级别4的主题功能将允许您选择除选择器中出现的最后一个元素以外的其他元素,但是(AFAIK)不允许您上下移动。

If you can modify your HTML 如果可以修改HTML

With this HTML structure ( .span1 actually moved to be adjacent): 使用此HTML结构( .span1实际上移动到相邻):

<div class="row-fluid">
    <div class="control-group control-field-nickname">
        <span class="span3 offset1">
            <label>{{ msgs.mid("nickname") }}</label>
        </span>
        <span class="span3 input-prepend">                              
            <i class="add-on icon-user"></i>
            <input class="form-field" type="email" id="field-ni
ckname">
            <span class="span1">Error!</span>
        </span>

    </div>
</div>

Then this CSS should work (for browsers that recognize the :invalid pseudo-class): 然后这个CSS应该工作(对于识别:invalid伪类的浏览器):

.span1 {
    display: none;
}

input:invalid + .span1 {
    display: inline-block; /* or block */
}

If you cannot restructure your HTML, then there is no CSS solution. 如果您无法重构HTML,那么就没有CSS解决方案。

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

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