LESS - 多个 &
说明
& 不仅表示最近的选择器,还表示所有父选择器。
示例
以下示例演示了如何使用 & 表示 LESS 文件中的所有父选择器 −
parent_selector.htm
<html>
<head>
<link rel = "stylesheet" href = "multiple1.css" type = "text/css" />
</head>
<body>
<div class = "grand_parent">
<h3>Welcome to TutorialsPoint</h3>
<p class = "parent"> It is possible to reference the
parent selector by using &(ampersand) operator.</p>
<p class = "parent_class">It is possible to reference the
parent selector by using &(ampersand) operator.</p>
</div>
</body>
</html>
接下来,创建 style.less 文件。
style.less
.grand_parent {
.parent {
& > & {
color: #A9F5F2;
}
& & {
color: #D0FA58;
}
&& {
color: #81BEF7;
}
&, &_class {
color: #A4A4A4;
}
}
}
您可以使用以下命令将 style.less 文件编译为 style.css −
lessc style.less style.css
执行上述命令;它将使用以下代码自动创建 style.css 文件 −
style.css
.grand_parent .parent > .grand_parent .parent {
color: #A9F5F2;
}
.grand_parent .parent .grand_parent .parent {
color: #D0FA58;
}
.grand_parent .parent.grand_parent .parent {
color: #81BEF7;
}
.grand_parent .parent,
.grand_parent .parent_class {
color: #A4A4A4;
}
输出
按照以下步骤查看上述代码的工作原理 −
将上述 html 代码保存在 parent_selector.htm 文件中。
在浏览器中打开此 HTML 文件,将显示以下输出。

less_parent_selectors.html