2017-04-01 154 views
1

有誰知道我可以如何使用變量CSS選擇器?例如,我希望我的CSS搶表id屬性這是變量...是這樣的:如何使用變量CSS選擇器

<table id="table_1"> 
    <thead> 
    </thead> 
    <tbody> 
    </tbody> 
</table> 

<table id="table_2"> 
    <thead> 
    </thead> 
    <tbody> 
    </tbody> 
</table> 

<table id="table_3"> 
    <thead> 
    </thead> 
    <tbody> 
    </tbody> 
</table> 

現在我想通過自己的id搶表:

table#[INSERT VARIABLE TABLE ID HERE]{ 
    margin: 0 auto; 
    border-collapse: collapse; 
    font-family: Agenda-Light, sans-serif; 
    font-weight: 100; 
    background: #333; color: #fff; 
    text-rendering: optimizeLegibility; 
    border-radius: 5px; 
} 

提前感謝!

+0

檢查了這個http://stackoverflow.com/questions/12155833/css-selector-id-contains-part-of-text – Sankara

+0

對於這個具體的例子:'table [id^= table_]'將選擇所有'

'具有以''table_「''開頭的'id'屬性的元素。 –

+0

不,這是不可能的,CSS沒有變量。您需要將其作爲一個類,然後使用JavaScript更改哪個表具有該類。 –

回答

2

如果所有的表都有相同的CSS,你可以使用類而不是id。

在css中不可能迭代或執行其他類型的邏輯。要使用變量,您可以使用像sass或更少的css預處理器,但是您必須「手動」定義每個變量。