W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
通過使用&運(yùn)算符,可以重復(fù)引用父選擇器,而不使用其名稱。 在選擇器&中可以多次使用。
下面的例子演示了在LESS文件中使用多個(gè)&:
<html> <head> <title>Parent Selector</title> <link rel="stylesheet" href="style.css" type="text/css" /> </head> <body> <h2>Welcome to W3Cschool</h2> <p class="select">It is possible to reference the parent selector by using &(ampersand) operator.</p> <p class="select_class1">It is possible to reference the parent selector by using &(ampersand) operator</p> </body> </html>
接下來,創(chuàng)建文件 style.less
.select { & + & { color: #A9F5F2; } & & { color: #D0FA58; } && { color: #81BEF7; } &, &_class1 { color: #A4A4A4; } }
您可以使用以下命令將 style.less 文件編譯為 style.css :
lessc style.less style.css
接下來執(zhí)行上面的命令,它將用下面的代碼自動(dòng)創(chuàng)建 style.css 文件:
.select + .select { color: #A9F5F2; } .select .select { color: #D0FA58; } .select.select { color: #81BEF7; } .select, .select_class1 { color: #A4A4A4; }
讓我們執(zhí)行以下步驟,看看上面的代碼如何工作:
將上述html代碼保存在 parent_selector.htm 文件中。
在瀏覽器中打開此HTML文件,將顯示如下輸出。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: