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. mysql_upgrade命令

    mysql 创建存储过程失败.查看错误日志,发现如下信息:*********************************************************************** ...

  2. 1015. Reversible Primes (20)

    the problem is from PAT,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1015 this pro ...

  3. 搭建属于自己的wiki

    1.开源wiki mediawiki http://www.mediawiki.org/wiki/MediaWiki 2. 开发文档 http://www.mediawiki.org/wiki/Man ...

  4. 命令行中使用adb安装apk

    转载:http://blog.sina.com.cn/s/blog_8324d8e80101b8dn.html 在你的android—IDE中找到D:\Softwave_Ghost\技术软件\IDE\ ...

  5. c语言例子递归与整数逆序

    例一 #include <stdio.h> //将一整数逆序后放入一数组中(要求递归实现) void convert(int *result, int n) { if(n>=10) ...

  6. 小米2s使用Hexamob Recovery PRO恢复数据

    这东西对于手机来说, 真是神器啊 现在很多手机都是以MTP连接到电脑的, 所以在PC上是看不到盘符, 也就无法使用finaldata 之类的工具恢复了. 而像小米2S这样的手机, 无法外接SD卡, 则 ...

  7. Web Navigation

    Description Standard web browsers contain features to move backward and forward among the pages rece ...

  8. json2使用方法

    例子1: //直接声明json数据结构 var myJSONObject = {"bindings": [ {"ircEvent": "PRIVMSG ...

  9. linux-搜索

    grep显示文件中的匹配行     grep  参数  字符串  文件名 -c 输出匹配的行数 -v输出不匹配行 不加参数  :输出匹配的行 经常和管道符一起使用 eg:ps -ef|grep tom ...

  10. js实现刮刮卡效果

    <!DOCTYPE html> <html> <body> <canvas/> <script> (function(bodyStyle) ...