2014-07-06 45 views
0

的最後一個孩子的財產我想刪除property from my css class for my last-child。下面是我的容器。刪除我的div

<div class="progress-stacked" id="progress-div"> 
    <div class="progress-bar progress-bar-fail"></div> 
    <div class="progress-bar progress-bar-fail"></div> 
</div> 

以下是我的css類。

.progress-bar-fail { 
    background-color: lightgrey; 
    background-image: none; 
    width: 9%; 
    margin-right: 11px; 
} 

如何從我的CSS類dynamically取消對我的最後一個孩子的margin-right財產。我嘗試了下面的東西,但它似乎不起作用。

$("#progress-div :last-child").css('margin-right', ''); 
+2

嘗試'$( 「#進步-DIV:最後的孩子」)的CSS( '保證金右', '汽車');' 「自動」 是利潤率默認值。 – Hardy

+0

爲什麼你使用jQuery做這件事,當CSS可以做到這一點? @TylerH:但是他會選擇錯誤的元素。 –

+0

@DavidThomas:你會如何在CSS中做到這一點? – Shane

回答

0

嘗試這樣

$("#progress-div div:last-child").css('margin-right', 0); 
0

您可以針對此,你在CSS中有兩個直接或jQuery中:

$(#進步-DIV .progress吧失效:最後-child).css('margin-right','initial');

爲什麼它不工作的值設置爲空字符串?因爲jQuery實際上並沒有修改你的css,所以它只是簡單的設置這些值。由於內聯樣式在級聯方面具有最高優先級,因此他們會覆蓋樣式標籤或外部樣式表中其他任何其他CSS。如果值已經是行內,或者是因爲你手動將其添加到您的標記或通過jQuery或Javascript,然後加入它有CSS屬性值設置爲在jQuery的一個空字符串實際上將刪除該值。

至於價值,如果你特別想保證金爲0,那麼當然作爲@weivall建議的值設置爲0,否則,我會建議使用初始該屬性設置爲它的默認值。 。