使用javascript打印HTML元素的内联样式值 [英] Printing inline style values of an HTML element using javascript

查看:34
本文介绍了使用javascript打印HTML元素的内联样式值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用javascript打印html元素的style属性值.我可以使用 document.getElementById('myId').style.property 获得特定的样式属性值,其中 property 类似于 width 高度

但是,如何获取元素的样式的完整列表?

解决方案

document.getElementById('myId').style.cssText 作为字符串,或 document.getElementById('myId').style 作为对象.

据我所知,这将返回实际"内联样式.在元素< a id ='myId'style ='font-size:inherit;'> 上, document.getElementById('myId').style.cssText 应该返回"font-size:inherit;" .如果那不是您想要的,请尝试 document.defaultView.getComputedStyle document.getElementById('myId').currentStyle (第一个是IE之外的所有内容,第二个是IE)一个仅适用于IE).请参阅此处,以了解有关计算样式和层叠样式的更多信息./p>

How can i print the value of style attribute of an html element using javascript. I can get a specific style property value using document.getElementById('myId').style.property, where property is something like width, height etc.

However, how can I get the entire list of styles for an element?

解决方案

document.getElementById('myId').style.cssText as a String, or document.getElementById('myId').style as an Object.

Edit:

As far as I can tell, this returns the "actual", inline style. On the element <a id='myId' style='font-size:inherit;'>, document.getElementById('myId').style.cssText should return "font-size:inherit;". If that is not what you want, try document.defaultView.getComputedStyle or document.getElementById('myId').currentStyle (the first one is all except IE, the second one is IE only). See here for more on computed vs. cascaded styles.

这篇关于使用javascript打印HTML元素的内联样式值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆