JavaScript 获取 CSS 样式
小于 1 分钟约 118 字
JavaScript 获取 CSS 样式
// 兼容所有浏览器
function getStyle(node, cssStyle) {
// IE获取 标准浏览器
return node.currentStyle ? node.currentStyle[cssStyle] : getComputedStyle(node)[cssStyle];
}
getComputedStyle(div)['width']; // 标准浏览器
div.currentStyle['width']; // 兼容 IE 浏览器
// div.style 只能获取到行间的 css 样式
div.style.cssText = 'width: 200px;height: 30px;'; // 设置 css 样式
div.style.getPropertyValue('width'); // 200px 取得宽度值
div.style.removeProperty('width'); // 删除 width 属性
// 取得计算后的 css 样式
div.style.cssText = 'width:200px';
div.style.getPropertyValue('width'); // 取得设置 cssText 的样式