2017-04-07 132 views
-2

假设我们有一个<label>其中有<span>兄弟姐妹。现在我想选择跨度并在其上应用一些样式。说:设计一个嵌套的HTML标签

<label class="checkbox-inline" for="currentDate"> 
               <input type="checkbox" checked="checked" id="currentDate" /> 
               <span><spring:message code='Admin.News.NewsDate.CurrentDate' /></span> 
              </label> 
              <span tooltip="<spring:message code='Admin.News.NewsDate.Tooltip' />" class="help-button">?</span> 

我想这有help-button,因为它的同胞类跨度chechbox-inline等级标识,获取padding-left: 0px;

+0

你想选择跨度只有当它是一个兄弟的div?或者是什么? – inorganik

+0

我觉得你的问题与此类似:http://stackoverflow.com/questions/1817792/is-there-a-previous-sibling-css-selector – otaviko

+0

请阅读更新 –

回答

0

https://developer.mozilla.org/en/docs/Web/CSS/General_sibling_selectorshttps://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_selectors

/*if you need to select the span, you can use the adjacent sibling (+) or general sibling (~) combinator*/ 
 

 
/*adjacent siblings*/ 
 
.checkbox-inline + .help-button{ 
 
    background-color: red; 
 
} 
 

 
/*general siblings*/ 
 
.checkbox-inline ~ .help-button{ 
 
    font-size: 2em; 
 
} 
 

 
    
 
/*if you need to select the label, you could use a parent structure, since sibling selectors only works on elements that come AFTER the sibling you specify*/ 
 
.parent .checkbox-inline{ 
 
background-color: blue; 
 
}
<div class="parent"> 
 

 
    <label class="checkbox-inline"> 
 
    <input type="checkbox" /> 
 
    </label> 
 
    <span class="help-button">?</span> 
 

 
</div>

所有的一切是说,它可能会更容易使用一个额外的ID或类要样式

+0

我不想要兄弟姐妹,我想要的元素本身。 –

+0

我编辑了我的答案,这是你在找什么? –