2013-10-16 36 views
0

如果層次結構如下所示:在SASS中,如何參考父母?

 
.all 
    .d1 
    .d2 

我要選擇「所有」到」 .D2" 。它會怎樣?例如:

 
.all 
    .d1 
    .d2 
     &:hover 
     & .all 
         ... 

行「& .all」來編譯.all ... ???

+1

你不能,想要這樣做並不合情理。沒有用於編譯的等效CSS。 – meagar

+0

你想要選擇'.all'中的每個元素,還是要選擇'.all:hover'?你能提供你期望的CSS嗎? – KatieK

+0

我想更改div「.all」。 – rplaurindo

回答

0

您只需定義想要的風格。所有下:

.all 
    background: #000 center center no-repeat 
    margin: 0 auto 
    .dl 
    background: #fff center center no-repeat 
    margin: 15px 
    &:hover 
     margin: 0 
     text-decoration: none 

這將輸出

.all { 
    background: #000 center center no-repeat; 
    margin: 0 auto; 
} 
.all .d1 { 
    background: #fff center center no-repeat; 
    margin: 15px; 
} 
.all .d1:hover { 
    margin: 0; 
    text-decoration: none; 
} 

編輯

我起先不明白你的問題。答案是這不是通過CSS可行的。你要使用jQuery來做到這一點是這樣的:

這樣的: http://jsfiddle.net/9ntg7/1/

SASS

.all 
     position: relative 
     display: block 
     height: 100px 
     width: 100px 
     z-index: 1 
     .dl 
     position: absolute 
     top: 50% 
     left: 50% 
     display: block 
     height: 50px 
     width: 50px 
     margin: -25px 0 0 -25px 
     z-index: 2 
     background: white 

    .all1 
     background: green 

    .all2 
     background: red 

的Javascript

$(".dl").hover(function() { 
    $(".all").removeClass(".all1") 
    $(".all").addClass("all2"); 
}); 
+0

這段代碼會改變「.d1」,但在事件「:hover」中沒有「.all」。 – rplaurindo

+0

你想說的.all:hover – aaronmallen

+0

不,「.d2:hover」修改「.all」。 – rplaurindo

0

你期望輸出什麼?在你給出的例子中,你會得到一個像這樣的選擇器:

.all .d1 .d2.all:hover 

......如果我知道SASS(我用LESS)。

這就是你想要的,或者你最終想要什麼?

+0

我想改變div「.all」。 – rplaurindo

0

如果你只是想做些什麼來<div class="all">,那麼你就可以做到這一點,例如:如果你想改變內部<div class="all">每一件事

.all 
    color: red 

,那麼你可以這樣做:

.all * 
    color: red 

最後,如果你想改變只有<div class="all">直接孩子,你可以這樣做:

.all > * 
    color: red 
+0

將沒有任何選項。我想通過div「.d2」上的事件「:hover」更改div「。all」,即父親「.d1」和此「.d2」。 – rplaurindo