2017-04-07 65 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

我編輯了我的答案,這是你在找什麼? –