我经常使用 CSS 通用选择器来重置 HTML 文档中的尺寸:
I often use the CSS universal selector to reset the dimensions in my HTML document:
* {
border: 0;
margin: 0;
padding: 0;
}
这也可以用 JavaScript 来完成吗?
Can this be done with JavaScript too?
对于普通的 HTML 元素,有 style 属性.但是如何与通用选择器对话呢?
For normal HTML elements there is the style property.
But how to speak to the universal selector?
getElementsByTagName("*") 将返回 DOM 中的所有元素.然后你可以为集合中的每个元素设置样式:
getElementsByTagName("*") will return all elements from DOM. Then you may set styles for each element in the collection:
var allElements = document.getElementsByTagName("*");
for (var i = 0, len = allElements.length; i < len; i++) {
var element = allElements[i];
// element.style.border = ...
}
这篇关于如何使用 JavaScript 设置通用 CSS 选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
:target 为空时的 CSS 选择器CSS selector when :target empty(:target 为空时的 CSS 选择器)
CSS 直接后代 (>) 在选择性方面没有任何价值Does the CSS direct decendant (gt;) not have any value in selectivity?(CSS 直接后代 (gt;) 在选择性方面没有任何价值吗?)
使用 querySelectorAll().方法返回的结果是否有序?Using querySelectorAll(). Is the result returned by the method ordered?(使用 querySelectorAll().方法返回的结果是否有序?)
Safari 错误:当使用 JS 删除项目时,first-child 不更Safari bug :first-child doesn#39;t update display:block when items are removed with JS(Safari 错误:当使用 JS 删除项目时,first-child 不更新
nth-子 CSS 选择器nth-Child CSS selectors(nth-子 CSS 选择器)
对多个 HTML 标签使用相同的 ID?Using same ID for multiple HTML tags?(对多个 HTML 标签使用相同的 ID?)