getComputedStyle加不加getPropertyValue()的区别

window.getComputedStyle(elem,null).getPropertyValue(“style”)和window.getComputedStyle(elem,null).style有什么区别

阅读 3.1k
1 个回答

window.getComputedStyle(elem,null)返回`CSSStyleDeclaration对象
这个对象有getPropertyValue`方法获取对应css属性的值
例如 getPropertyValue('color');//获取字体颜色子

CSSStyleDeclaration是没有style属性的

你是不是想问
window.getComputedStyle(elem,null).getPropertyValue(“color”)和window.getComputedStyle(elem,null).color有什么区别
有区别,属性值是不同的访问方式
在访问例如background-color类似格式的css属性时
`window.getComputedStyle(elem,null).background-color就不可以了,需要使用
window.getComputedStyle(elem,null).backgroundColor访问
window.getComputedStyle(elem,null).getPropertyValue(“background-color”)是可以的

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题