Skip to content
此页目录
本文总阅读量

JavaScript 获取 CSS 样式

js
// 兼容所有浏览器

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 的样式

评论

交流群