通过obj.style的方式只能取得"内联style"的值,对于<style></style>中的css属性值,则无能为力 。

我们可以用obj.currentStyle,window.getComputedStyle()来获取

意: 只有 IE Opera 支持使用 currentStyle 获取 HTMLElement 的计算后的样式, 其他浏览器中不支持。

标准浏览器中使用getComputedStyleIE9及以上也支持getComputedStyle

window.getComputedStyle(obj,伪元素)
参数说明:
第一个参数为要获取计算后的样式的目标元素
第二个参数为期望的伪元素, 如 ':after'':first-letter' ,一般设为null

考虑兼容性,封装函数
function getStyle (el,attr){
return el.currentStyle?el.currentStyle[attr]:getComputedStyle(el,null)[attr];
}
注意: 2个方法,获取的对象是只读,要改样式,还得靠obj.style

相关文章:

  • 2021-12-07
  • 2022-02-27
  • 2022-12-23
  • 2022-12-23
  • 2022-03-10
  • 2021-10-07
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-01
  • 2021-12-09
相关资源
相似解决方案