JavaScript 获取 CSS 样式

1// 兼容所有浏览器
2
3function getStyle(node, cssStyle) {
4  // IE获取  标准浏览器
5  return node.currentStyle ? node.currentStyle[cssStyle] : getComputedStyle(node)[cssStyle]
6}
7
8getComputedStyle(div).width // 标准浏览器
9div.currentStyle.width // 兼容 IE 浏览器
10
11// div.style 只能获取到行间的 css 样式
12div.style.cssText = 'width: 200px;height: 30px;' // 设置 css 样式
13div.style.getPropertyValue('width') // 200px 取得宽度值
14div.style.removeProperty('width') // 删除 width 属性
15
16// 取得计算后的 css 样式
17div.style.cssText = 'width:200px'
18div.style.getPropertyValue('width') // 取得设置 cssText 的样式