js中getComputedStyle()与currentStyle()、style()方法的区别

1. obj.style:这个方法只能获取行内样式写在style属性中的值(style=”…”),而无法获取定义在<style type="text/css"></style>里面的属性。

2.obj.currentStyle和getComputedStyle 方法相同,但obj.currentStyle只能用于IE浏览器

getAttribute()用以获取HTML元素的属性(如id,name,type以及其他自定义属性)

style对象的属性(如background,color,border等等)用以设置元素的样式

3.getComputedStyle和currentStyle只能获取属性值,无法设置属性。如果想设置属性值,可是使用ob.style.attr.

最后补充一点(获取非行内样式的兼容写法

function getStyle(obj, attr) {
    if (obj.currentStyle) {
        return obj.currentStyle[attr];
    } else {
        return getComputedStyle(obj, false)[attr];
    }
}
如果文字内容比较难以理解,可参考下图表格:

posted on 2020-06-06 00:18  被窝暖暖嘻嘻嘻  阅读(325)  评论(0编辑  收藏  举报

导航