使用javascript打印HTML元素的内联样式值
Printing inline style values of an HTML element using javascript
如何使用javascript打印html元素的style属性的值。我可以使用document.getElementById('myId').style.property
获得特定的样式属性值,其中property
是width
, height
等。
但是,我如何获得一个元素的整个样式列表呢?
document.getElementById('myId').style.cssText
作为String,或document.getElementById('myId').style
作为Object。
据我所知,这将返回"实际的"内联样式。对于元素<a id='myId' style='font-size:inherit;'>
, document.getElementById('myId').style.cssText
应该返回"font-size:inherit;"
。如果这不是您想要的,请尝试document.defaultView.getComputedStyle
或document.getElementById('myId').currentStyle
(第一个是除IE之外的所有,第二个是IE)。有关计算样式和级联样式的更多信息,请参阅此处。
<div id="x" style="font-size:15px">a</div>
<script type="text/javascript">
function getStyle(oElm, strCssRule){
var strValue = "";
if(document.defaultView && document.defaultView.getComputedStyle){
strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
}
else if(oElm.currentStyle){
strCssRule = strCssRule.replace(/'-('w)/g, function (strMatch, p1){
return p1.toUpperCase();
});
strValue = oElm.currentStyle[strCssRule];
}
return strValue;
}
// get what style rule you want
alert(getStyle(document.getElementById('x'), 'font-size'));
</script>
哎呀…就像
一样简单element.style.cssText
和跨浏览器
http://jsfiddle.net/4F2RQ/这应该会转储对象:下面是一个示例
编辑:这有点奇怪:
for (var prop in styles) {
console.log(styles[prop], styles[styles[prop]]);
}
相关文章:
- insertRule不插入样式元素
- 具有描边属性的 SVG 样式 G 元素
- 如何在样式元素中获取 CSS 代码
- 如何让用户更改表单的样式元素
- 如何根据子组件的状态更改父组件样式元素
- 如何使样式元素的一部分成为函数?(IE:颜色:$random而不是颜色:红色)
- 动态创建的样式元素
- 如何使用javascript获取外部样式元素的css属性
- 在ExtJS中为一个项目添加多个样式元素
- 在目标页面加载完成之前添加样式元素
- 将样式元素动态插入DOM的最后一种跨浏览器方式
- CKEditor 剥离 Rails 中跨度类的样式元素
- 根据单元格内容更改样式元素
- 更改绝对样式元素的位置
- 在React组件中呈现样式元素
- 样式元素左置:页面的x px
- 按钮样式元素的类冲突
- Javascript样式元素
- 在移动设备中,哪一个更快:包括一个额外的CSS文件或在JavaScript中创建一个样式元素
- 需要一种方法来样式元素,我不能访问