你更不能轉換爲下面的CSS沒有任何錯誤:
td input[type=text] {
width: 100px;
}
td input[type=text]:nth-child(2) {
width: 40px;
}
但是,如果您有其他元素的文本輸入的兄弟姐妹,它們可以與:nth-child()
聲明干擾,爲:nth-child()
不僅外觀在一個元素相對於同一父類中的所有其他同胞的位置上,而不僅僅是其他類型的元素(即input[type=text]
)。例如,如果您有第二個孩子的label
,那麼您的輸入將不再是第二個孩子,因爲該位置已被標籤佔據。
如果您有td
中唯一的輸入是所有的[type=text]
你應該能夠使用:nth-of-type()
,而不是逃脫:
// LESS
td {
input[type=text] {
width: 100px;
&:nth-of-type(2) {
width: 40px;
}
}
}
/* CSS */
td input[type=text] {
width: 100px;
}
td input[type=text]:nth-of-type(2) {
width: 40px;
}
請記住,雖然,它只是着眼於元素名稱input
而不是[type=text]
屬性!
或者,如果你知道你只有兩個文本輸入框,你可以使用一般的兄弟選擇,而不是搶的是遵循先輸入之一:
// LESS
td {
input[type=text] {
width: 100px;
& ~ input[type=text] {
width: 40px;
}
}
}
/* CSS */
td input[type=text] {
width: 100px;
}
td input[type=text] ~ input[type=text] {
width: 40px;
}
沒有時間使用LESS進行測試,但可能的解決方法是使用'input [type = text]:nth-child(2)'而不是嵌套。然後,如果它仍然不起作用,這是選擇器的錯誤。如果確實如此,那麼它可能是您正在使用的LESS程序中的一個錯誤。 – 0b10011 2012-07-19 03:49:24