我被要求编写一个使用CSS通用选择器的示例脚本。我该怎么做?我知道这是the universal selector in CSS:
SupportedOrientations
示例代码:
* { //Universal Selector '*' to set/reset property values
margin: 0;
padding: 0;
}
单击时,该按钮应将元素的所有边距和填充重置为0.但相反,它仅对主体执行此操作。
澄清:具体来说,我需要一个javascript方法,将所有元素的所有边距和填充设置为0。
答案 0 :(得分:2)
只需使用jQuery' $('*')
或document.querySelectorAll("*")
。
答案 1 :(得分:0)
以下是一个简单的示例,它会遍历正文的元素。
<!DOCTYPE html>
<html>
<body>
<h2 class="example" style="padding:50px;" >A heading with class="example"</h2>
<p>Click the button to activate the universal selector.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var els = document.body.querySelectorAll("*");
for (var i = 0; i < els.length; i++) {
els[i].style.margin = 0;
els[i].style.padding = 0;
}
}
</script>
</body>
</html>