style、currentStyle、getComputedStyle区别介绍

来自:蓝色天空

样式表有三种方式

  1. 内嵌样式(inline Style) :是写在Tag里面的,内嵌样式只对所有的Tag有效。
  2. 内部样式(internal Style Sheet):是写在HTML的<head></head>里面的,内部样式只对所在的网页有效。
  3. 外部样式表(External Style Sheet):如果很多网页需要用到同样的样式(Styles),将样式(Styles)写在一个以.css为后缀的CSS文件里,然后在每个需要用到这些样式(Styles)的网页里引用这个CSS文件。

  最常用的是style属性,在JavaScript中,通过document.getElementById(id).style.XXX就可以获取到XXX的值,但意外的是,这样做只能取到通过内嵌方式设置的样式值,即style属性里面设置的值。

  style              标准的样式!可能是由style属性指定的!
     runtimeStyle   运行时的样式!如果与style的属性重叠,将覆盖style的属性!
  currentStyle   指 style 和 runtimeStyle 的结合!

通过currentStyle就可以获取到通过内联或外部引用的CSS样式的值了(仅限IE

如:document.getElementById("test").currentStyle.top

要兼容FF,就得需要getComputedStyle 出马了

注意:getComputedStyle是firefox中的,

currentStyle是ie中的.

例:

<style>
#mydiv {
width : 300px;
}
</style>
var mydiv = document.getElementById('mydiv');
if(mydiv.currentStyle) {
var width = mydiv.currentStyle['width'];
alert('ie:' + width);
} else if(window.getComputedStyle) {
var width = window.getComputedStyle(mydiv , null)['width'];
alert('firefox:' + width);
}

另外在FF下还可以通过下面的方式获取

document.defaultView.getComputedStyle(mydiv,null).width
window.getComputedStyle(mydiv , null).width

style、currentStyle、getComputedStyle区别介绍的更多相关文章

  1. javascript 读取内联之外的样式(style、currentStyle、getComputedStyle区别介绍) (转载)

    样式表有三种方式: 内嵌样式(inline Style) :是写在Tag里面的,内嵌样式只对所有的Tag有效.   (也称作“内联样式”) 内部样式(internal Style Sheet):是写在 ...

  2. js便签笔记(7)——style、currentStyle、getComputedStyle区别介绍【转载】

    转者语: 今天看jQuery源码CSS部分,里面用到了currentStyle和getComputedStyle来获取外部样式. 因为elem.style.width只能获取elem的style属性里 ...

  3. style currentStyle getComputedStyle的区别和用法

    先介绍下层叠样式表的三种形式: 1.内联样式,在html标签中style属性设置. <p style="color:#f90">内联样式</p> 2.嵌入样 ...

  4. 元素高度、宽度获取 style currentStyle getComputedStyle getBoundingClientRect

    1.示例代码 (1)html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  5. 用JS查看修改CSS样式(cssText,attribute('style'),currentStyle,getComputedStyle)

    CSS样式定义方法 大家都知道,在为HTML设置样式的时候,通常有三种方法:内联样式,内部样式表,外部样式表. 1.内联样式: 内联样式表就是在HTML元素中的行内直接添加style属性. <d ...

  6. 浅谈style.,currentStyle,getComputedStyle,getAttribute

    xxx为属性. ele为元素. 1.style.是针对于样式 在前面的一篇博客中我也有说到,ele.style.xxx; 通常用于赋值,赋值也是针对于行内样式,用它来取值的话,它只能取到内联样式. 今 ...

  7. 关于对象.style currentstyle 的区别

    对象.style的方式只能获取行内写法的样式,但是外部引入的或者写在head里面的就无法获取,只能用currentstyle.

  8. style、currentStyle、getComputedStyle(不同浏览器获取css样式)区别介绍

    style.currentStyle.getComputedStyle区别介绍   样式表有三种方式 内嵌样式(inline Style) :是写在Tag里面的,内嵌样式只对所有的Tag有效. 内部样 ...

  9. obj.style 和currentstyle 等区别

    版权声明:本文为博主原创文章,未经博主允许不得转载. 获取样式  obj.style   和currentstyle  等区别   obj.style只能获得内嵌样式(inline Style)就是写 ...

随机推荐

  1. laraval框架之数据库不可不吐槽的坑

    最近做的项目一直在用laraval框架,有些地方确实很方便,但是有些方面实在是太坑了,就比如这次在数据库里,官方文档写的是 Take note that email is not a required ...

  2. Explain的type, where 和 order by 组合是索引的选择

    Explain的type显示的是访问类型,是较为重要的一个指标,结果值从好到坏依次是: system > const > eq_ref > ref > fulltext > ...

  3. linux自带抓包工具tcpdump使用说明

    tcpdump是个强大的网络分析工具,有很多细致的规则可以定义. 参考:http://www.cnblogs.com/ggjucheng/archive/2012/01/14/2322659.html ...

  4. Pretty Poem

    Poetry is a form of literature that uses aesthetic and rhythmic qualities of language. There are man ...

  5. IDL中File_Search函数用法详解(转)

    来自:http://blog.sina.com.cn/s/blog_764b1e9d01014ajp.html 在利用IDL进行批处理时,通常用到file_search函数进行输入路径文件的搜索,现根 ...

  6. android开发之路07(无硝烟的战争)

    如何做一名优秀的android面试官? 如何做一名优秀的android候选者? 提到这个问题我不得不提起我们小升初,初升高,高生升本这几个历程中我们与出题人之间的无硝烟的战争.我们总是为自己的成绩担心 ...

  7. spl_autoload_register()和__autoload()区别

    这篇文章主要介绍了spl_autoload_register()和__autoload()区别,需要的朋友可以参考下   关于spl_autoload_register()和__autoload(), ...

  8. ubuntu14_gtk 安装

    1:apt-get install build-essential2:apt-get install gnome-devel gnome-devel-docs

  9. 【数论,思路】HDU-5288;多校#1-1001

    2015 Multi-University Training Contest 1  1001 /* Problem: HDU-5288,多校#1 1001 Tips: 数学.思路 Date: 2015 ...

  10. 【数论】UVa 11526 - H(n)

    What is the value this simple C++ function will return? long long H(int n) { ; ; i <= n; i=i+ ) { ...