JavaScript 获取样式
是否可以使用 JavaScript 获取对象的所有样式?像这样的东西:
main.css
-------
#myLayer {
position: absolute;
width: 200px;
height: 100px;
color: #0000ff;
}
main.js
-------
var ob = document.getElementById("myLayer");
var pos = ob.(getPosition);
// Pos should equal "absolute" but
// ob.style.position would equal null
// any way to get absolute?
Is it possible to get ALL of the styles for an object using JavaScript? Something like:
main.css
-------
#myLayer {
position: absolute;
width: 200px;
height: 100px;
color: #0000ff;
}
main.js
-------
var ob = document.getElementById("myLayer");
var pos = ob.(getPosition);
// Pos should equal "absolute" but
// ob.style.position would equal null
// any way to get absolute?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在谈论所谓的计算样式,请查看以下文章以了解如何获取它:
从上一篇文章来看,这里有一个函数:
如何使用它:
CSS:
JS:
You are talking about what is known as Computed Style, check out these article on how to get it:
From the last article, here is a function:
How to use it:
CSS:
JS:
您可能会使用:
每个 CSS 属性都有它自己的对象模型。通常那些包含'-'的css属性是使用java模型编写的。
例如:
如果您想获取为某个对象定义的所有 css 属性,则必须将它们一一列出,因为即使您没有在样式表中设置该属性,对象也会将其与默认值。
You might use:
Every CSS property has it's own object model. Usually those css properties that contain '-' are written using java model.
For example:
If you want to get all the css properties that are defined for an object, you will have to list them one by one, because even if you did not set the property in your style sheet, an object will have it with the default value.
Polyfill 使用 javascript 获取元素的当前 CSS 样式...访问 链接 了解更多信息
Polyfill to get the current CSS style of element using javascript ... Visit the link for more info