getComputedStyle与currentStyle
currentStyle:获取计算后的样式。也叫当前样式、终于样式。
长处:能够获取元素的终于样式。包含浏览器的默认值,而不像style仅仅能获取行间样式。所以更经常使用到。
注意:不能获取复合样式如background属性值。仅仅能获取单一样式如background-color等。
alert (oAbc.currentStyle);
很遗憾的是,这个好使的东西也不能被各大浏览器完美地支持。准确地说,在我測试的浏览器中,IE8和Opera 11弹出了“object CSSStyleDeclaration”;FF 12、chrome 14、safari 5则弹出“undefined”。
尽管currentStyle无法适用于全部浏览器,可是能够依据上面的測试的结果来区分开支持、不支持的浏览器,然后再找到兼容的写法。
var oAbc = document.getElementById("abc");
if(oAbc.currentStyle) {
//IE、Opera
alert("我支持currentStyle");
} else {
//FF、chrome、safari
alert("我不支持currentStyle");
}
事实上在FF浏览器中我们能够使用getComputedStyle(obj,false)来达到与IE下currentStyle同样的效果。
getComputedStyle(obj,false):在FF新版本号中仅仅须要第一个參数。即操作对象,第二个參数写“false”也是大家通用的写法,目的是为了兼容老版本号的火狐浏览器。
兼容写法:
var oAbc = document.getElementById("abc");
if(oAbc.currentStyle) {
//IE、Opera
//alert("我支持currentStyle");
alert(oAbc.currentStyle.width);
} else {
//FF、chrome、safari
//alert("我不支持currentStyle");
alert(getComputedStyle(oAbc,false).width);
}
一个空白页面中body的id=”abc”,測试以上代码,IE和Opera弹出“auto”,其他三款浏览器则弹出“***px”。尽管结果不同。可是能够发现chrome和safari也都和火狐一样,顺利地读取到了属性值。不支持currentStyle的三款浏览器(FF、chrome、safari),都是支持getComputedStyle的。
结果表明:对浏览器是否支持currentStyle的推断 + getComputedStyle,就能够做到兼容各主流浏览器的效果。并且兼容写法并不复杂,你掌握了吗?^_^
支持currentStyle:IE、Opera
支持getComputedStyle:FireFox、Chrome、Safari
注意最后的弹出内容。currentStyle返回浏览器的默认值”auto”。而getComputedStyle则返回详细的值”**px”。这应该是两者的一个小差异。有兴趣的童鞋能够一起交流研究下。
getComputedStyle与currentStyle的更多相关文章
- getComputedStyle()与currentStyle
getComputedStyle()与currentStyle计算元素样式 发表于 2011-10-27 由 admin “DOM2级样式”增强了document.defaultView,提供了get ...
- 获取css样式,style、getComputedStyle及currentStyle的区别
样式表有三种: 内嵌样式:<div id="box" style="color:red">box</div>,style写在html中的 ...
- getComputedStyle(and currentStyle)
1.getComputedStyle 1.1 用法: currentStyle获取计算后的样式,也叫当前样式.最终样式.优点:可以获取元素的最终样式,包括浏览器的默认值,而不像style只能获取行间样 ...
- getComputedStyle和currentStyle
/*alert(div.style.width)*/ //null function getstyle(obj,name){ if(obj.currentStyle) { return obj.cur ...
- getComputedStyle与currentStyle获取样式(style/class)
今天看jQuery源码CSS部分,里面用到了currentStyle和getComputedStyle来获取外部样式. 因为elem.style.width只能获取elem的style属性里的样式,无 ...
- getComputedStyle与currentStyle获取样式
转载自:https://segmentfault.com/a/1190000007477785 CSS的样式分为三类: 内嵌样式:是写在标签里面的,内嵌样式只对所在的标签有效内部样式:是写在HTML里 ...
- JS中使用document.defaultView.getComputedStyle()、currentStyle()方法获取CSS属性值
在对网页进行调试的过程中,经常会用到js来获取元素的CSS样式,方法有很多很多,现在仅把我经常用的方法总结如: 1. obj.style:这个方法只能JS只能获取写在html标签中的写在style属性 ...
- JS获取元素属性、样式getComputedStyle()和currentStyle方法兼容性问题
1. getComputedStyle() 方法获取到的是经过计算机/浏览器计算后的样式 getComputedStyle($("#div")).width; 兼容性:IE6 7 ...
- getComputedStyle与currentStyle获取元素当前的css样式
CSS的样式分为三类: 内嵌样式:是写在标签里面的,内嵌样式只对所在的标签有效内部样式:是写在HTML里面的,内部样式只对所在的网页有效外部样式表:如果很多网页需要用到同样的样式,将样式写在一个以.c ...
随机推荐
- ODBC、OLE DB、 ADO、ODAC、ODP.NET
面对各式各样.越来越多的数据来源和访问需求.软件开发框架中一般都提供了统一的访问接口和方法,来屏蔽数据库底层差异. 各式各样的Provider提供者. ODBC(Open Database Conne ...
- QT设置前景图位置(配色简单漂亮)
QPushButton { background-image: url(:/Resources/green_click.png); image: url(:/Resources/toolsbutton ...
- Qt下如何修改文件的时间(全平台修改)
提供一个全平台修改文件的时间的方法,希望大家喜欢 /* UTIME.C: This program uses _utime to set the * file-modification time to ...
- china-pub
#!/usr/bin/env python #coding:utf-8import urllib2,re,sys,os,types ...
- IT第八天 - 类的应用、debug、项目开发模式优化
IT第八天 上午 类的应用 1.对象在实例化时是非常耗费系统资源的,因此要尽量减少new字段的使用 2.类的初始值是null,在使用未实例化的类时,很容易导致报错:NullExceptionPoint ...
- UVA 10652 Board Wrapping(凸包)
The small sawmill in Mission, British Columbia, hasdeveloped a brand new way of packaging boards for ...
- Memcached初体验及原理解说
1.简单介绍 Memcached 是一个 高性能的 分布式 内存对象缓存系统,用于动态Web应用降低数据库负载,提升性能. 2.试用场景 1.变化频繁,具有不稳定性的数据 (比方用户在线状态.在线人数 ...
- C++之字符串分割函数split
c++之字符串分割: /* *c++之字符串分割: */ #include <iostream> #include <string> #include <vector&g ...
- akka actor 的request-response简单实现
注:本文章是看blog后的一个阶段小结,只作为个人笔记, 原文链接:http://www.iteblog.com/archives/1154 官网地址贴上:http://doc.akka.io/doc ...
- Python相关项目和技术
下面的项目是<Learn PYTHON the hard way>里面的,以后可能会补充: 1.Django,创建web程序的框架:https://www.djangoproject.co ...