2017-05-15 140 views
1

我有HTML这样的:是否有处理选择“的元素包含类的元素”

<div class="form-group"> 
    <input type="email" class="form-control> 
    <div class="form-error">This is not valid email address</div> 
</div> 

我打印div.form-error动态如果输入有错误。

如果在div.form-group中有div.form-error,我需要CSS代码才能使输入的边框变红。

我正在与SASS合作,也许它有一个功能来做到这一点。我正在寻找这样的东西:

.form-group { 
    &:contains('.form-error') { 
     input { 
      border: 1px solid red; 
     } 
    } 
} 

我该怎么办?

+1

我会试着把'form-error'(或一个替代的类名)放在'.form-group'元素本身上,而不是它的子元素上(我假设添加了类名是使用服务器端脚本或在客户端使用JavaScript完成的,其中任何一个都可以处理这个)。然而不幸的是,对于纯粹的CSS,不可能根据它的后代(或后继的兄弟)对祖先(或之前的兄弟)进行样式设计。 –

+0

向父项添加一个类会容易得多,因为您可以轻松控制子项中的所有内容 – Huangism

回答

1

好了,而我的意见仍然有效–这是不可能使用CSS样式以前的兄弟姐妹,或祖先的基础上,后续的兄弟姐妹或后代–有一个方法。虽然它确实需要改变你的HTML,它仍然会模拟你在问题中发布的元素顺序。

这就是说,HTML从改变后:

<div class="form-group"> 
    <input type="email" class="form-control" /> 
    <div class="form-error">This is not valid email address</div> 
</div> 

要:

<div class="form-group"> 
    <div class="form-error">This is not valid email address</div> 
    <input type="email" class="form-control" /> 
</div> 

.form-group { 
 
    /* to use the flex layout model: */ 
 
    display: flex; 
 

 
    /* to have the elements arranged 
 
    in a column instead of a row 
 
    (which you may or may not want): */ 
 
    flex-direction: column; 
 

 
    /* aesthetics, adjust to taste: */ 
 
    width: 50%; 
 
    border: 1px solid #000; 
 
    margin: 0 0 0.5em 0; 
 
} 
 

 
.form-group .form-error { 
 
    /* places the .form-error element(s) 
 
    at the end of the layout in position 
 
    2, which causes the <input> to take 
 
    position 1; note that the elements 
 
    are still in the same place for CSS 
 
    selection: */ 
 
    order: 2; 
 
} 
 

 

 
/* This styles the email <input> element if 
 
    it follows the .form-error element: */ 
 
.form-error + input[type=email] { 
 
    border-color: red; 
 
}
<div class="form-group"> 
 
    <div class="form-error">This is not a valid email address</div> 
 
    <input type="email" class="form-control" /> 
 
</div>

另外值得注意的是,根据caniuse.com,Flexbox的目前全部可用浏览器;尽管您的用户可能没有将自己的浏览器更新为当前(或之前)版本。

有,当然,你可以做到这一点只需使用:invalid伪类的其他方式:

/* Selects any <input> element whose value is invalid: */ 
 
input:invalid { 
 
    border-color: red; 
 
}
<div class="form-group"> 
 
    <input type="email" class="form-control" /> 
 
    <div class="form-error">This is not a valid email address</div> 
 
</div>

,当然,你甚至可以用衰落来显示或隐藏错误消息:

/* Selects any <input> element whose value is invalid: */ 
 

 
input:invalid { 
 
    border-color: red; 
 
} 
 

 
/* hides the .form-error element using the opacity 
 
    property which takes a value, which allows it 
 
    to be animated from hidden (opacity: 0) to shown 
 
    (opacity: 1): */ 
 
.form-error { 
 
    /* visually hidden: */ 
 
    opacity: 0; 
 
    /* specifies that the opacity property should be 
 
    transitioned over a 0.3 second time in a linear 
 
    animation: */ 
 
    transition: opacity 0.3s linear; 
 
} 
 
/* Selects the .form-error element that immediately 
 
    follows an <input> which is :invalid */ 
 
input:invalid+.form-error { 
 
    opacity: 1; 
 
}
<div class="form-group"> 
 
    <input type="email" class="form-control" /> 
 
    <div class="form-error">This is not a valid email address</div> 
 
</div>

参考书目:

0

正如人们在评论中正确指出的那样,您需要改变标记以使其发挥作用。您可以使用sibling选择: https://developer.mozilla.org/en/docs/Web/CSS/Adjacent_sibling_selectors

.form-group { 
    .form-error + input { 
     border: 1px solid red; 
    } 
} 

.form-error + input { 
 
    border: 1px solid red; 
 
}
<div class="form-group"> 
 
    <div class="form-error">This is not valid email address</div> 
 
    <input type="email" class="form-control" /> 
 
</div>

+0

我认为只有在'input'之前的'.form-error'为_immediately_之前''''选择器才会工作,提供标记其之前没有。 – zgood

+0

“*还有一般的兄弟选择器用于恢复树*” - 不,不存在:*没有*,根本没有组合器,至今,将“*返回树*”。 –

+0

为了清晰起见,我已经重新录制了。我之前使用过这个表单元素 - 例如为一个焦点输入样式化一个标签。 –

0

您可以使用child selectorssibling selectors

对于示例:

.form-group { 
    .form-error ~ input { 
     border: 1px solid red; 
    } 
} 
+1

我假设一般兄弟组合者在SASS中以不同方式工作,不知怎的,因为如果不是这样不行的话。 –

+1

在为兄弟选择器提供的链接上,清楚地表明目标需要在之后而不是之前。所以'.form-error〜input'将不起作用,因为输入在错误之前https://jsfiddle.net/njfcocpx/1/ – Huangism

相关问题