CSSOM之getComputedStyle,currentStyle,getPropertyValue,getAttribute
js关于CSSOM编程的样式相关几个常用的方法
webkit:getComputedStyle,getPropertyValue
IE:currentStyle,getAttribute
前言
jquery 中的 css() 方法,其底层运用的就是 getComputedStyle,getPropertyValue 方法。
getComputedStyle
getComputedStyle 是一个可以获取当前元素所有最终使用的css属性值的方法。返回一个CSSStyleDeclaretion 实例的对象。只读
语法如下:
var style = window.getComputedStyle("元素", "伪类");
例如:
var dom = document.getElementById("test"),
style = window.getComputedStyle(dom , ":after");
getComputedStyle与style的区别
我们使用element.style
也可以获取元素的CSS样式声明对象,但是其与getComputedStyle
方法还有有一些差异的。
1.element.style 属性可读可写
2.getComputedStyle 返回的是元素最终的样式,而element.style 只是style属性的值
注意:getComputedStyle 方法在 window ,和document.defaultView 上
window.getComputedStyle===document.defaultView.getComputedStyle 返回True.
getComputedStyle与currentStyle
currentStyle
是IE浏览器自娱自乐的一个属性,其与element.style
可以说是近亲,至少在使用形式上类似,element.currentStyle
,差别在于element.currentStyle
返回的是元素当前应用的最终CSS属性值(包括外链CSS文件,页面中嵌入的<style>
属性等)。
因此,从作用上将,getComputedStyle
方法与currentStyle
属性走的很近,形式上则style
与currentStyle
走的近。不过,currentStyle
属性貌似不支持伪类样式获取,这是与getComputedStyle
方法的差异,也是jQuery css()
方法无法体现的一点。
getPropertyValue
getPropertyValue
方法可以获取CSS样式申明对象上的属性值(直接属性名称),例如:
window.getComputedStyle(element, null).getPropertyValue("float");
如果我们不使用getPropertyValue
方法,直接使用键值访问,其实也是可以的。但是,比如这里的的float
,如果使用键值访问,则不能直接使用getComputedStyle(element, null).float
,而应该是cssFloat
与styleFloat
,自然需要浏览器判断了,比较折腾!
使用getPropertyValue
方法不必可以驼峰书写形式(不支持驼峰写法),例如:style.getPropertyValue("border-top-left-radius")
;
兼容性getPropertyValue
方法IE9+以及其他现代浏览器都支持,
OK,一涉及到兼容性问题(IE6-8肿么办),感觉头开始微微作痛了~~,不急,IE自由一套自己的套路,就是getAttribute
方法
getPropertyValue和getAttribute
在老的IE浏览器(包括最新的),getAttribute
方法提供了与getPropertyValue
方法类似的功能,可以访问CSS样式对象的属性。用法与getPropertyValue
类似:
style.getAttribute("float");
综上,获取元素的兼容性样式:
var oButton = document.getElementById("button"); if (oButton) {
oButton.onclick = function() {
var oStyle = this.currentStyle? this.currentStyle : window.getComputedStyle(this, null);
if (oStyle.getPropertyValue) {
alert("getPropertyValue下背景色:" + oStyle.getPropertyValue("background-color"));
} else {
alert("getAttribute下背景色:" + oStyle.getAttribute("backgroundColor"));
}
};
}
CSSOM之getComputedStyle,currentStyle,getPropertyValue,getAttribute的更多相关文章
- getComputedStyle/currentStyle/style之间的爱恨情仇
getComputedStyle是? getComputedStyle是一个可以获取当前元素所有最终使用的CSS属性值.返回的是一个CSS样式声明对象([object CSSStyleDeclarat ...
- 【笔记】归纳js getcomputedStyle, currentStyle 以及其相关用法
好吧,鉴于前端则个行业知识宽度广而深,早期看过高程介绍过的获取元素计算后的最终样式(浏览器显示的最终样式)的方法现在也忘得七七八八了 于是百度了一下,看了一下大神张鑫旭的博客,这里写个随笔记录一下 ...
- .style, .getComputedStyle(),.currentStyle区别
1)style只能获取行间样式(写在标签里面的):能读能写 2)currentStyle是专属ie的属性,区别他返回的是最终样式 及包括行间和外链css 3)getComputedStyle是一个可以 ...
- Js获取元素样式值(getComputedStyle¤tStyle)兼容性解决方案
因为:style(document.getElementById(id).style.XXX)只能获取元素的内联样式,内部样式和外部样式使用style是获取不到的. 一般js获取内部样式和外部样式使用 ...
- 样式计算的几种方式与兼容写法:getComputedStyle¤tStyle&style
window.getComputedStyle(element,[string]) 1参为需要获取样式的元素,2参指定伪元素字符串(如“::after”,不需要则为null),设置2参可获取eleme ...
- getComputedStyle() 和 getPropertyValue()
// getComputedStyle() 方法用于获取指定元素的 CSS 样式. // 获取的样式是元素在浏览器中最终渲染效果的样式. // getPropertyValue() 方法返回指定的 C ...
- 获取元素计算样式getComputedStyle()与currentStyle
window.getComputedStyle()方法是标准化接口,返回一个对象,该对象在应用活动样式表并解析这些值可能包含的任何基本计算后报告元素的所有CSS属性的值. 私有的CSS属性值可以通过对 ...
- 【CSS进阶】原生JS getComputedStyle等方法解析
最近一直在研读 jQuery 源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,让你感叹代码之美. 其结构明晰,高内聚.低耦合,兼具优秀的性能与便利的扩展性,在浏览器的兼容性(功能缺陷.渐 ...
- 获取元素CSS值之getComputedStyle方法熟悉
by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=2378 一.碎碎念~前 ...
随机推荐
- smarty模板原理和增删改查例题
Smarty模板:(前后端分离)原理:核心是一个类,先修改配置文件,在使用的时候引入配置文件即可,(init.inc.php)$smarty->assign("ceshi", ...
- (转)python爬取拉勾网信息
学习Python也有一段时间了,各种理论知识大体上也算略知一二了,今天就进入实战演练:通过Python来编写一个拉勾网薪资调查的小爬虫. 第一步:分析网站的请求过程 我们在查看拉勾网上的招聘信息的时候 ...
- 如何获取imageView中当前内容的相关信息并比较?
public class MainActivity extends Activity implements OnClickListener{ private Button button; privat ...
- dns服务
http://33024.blog.163.com/blog/static/12307042220119179237568/
- C#使用ajaxForm进行上传图片
<div class='imgOuter addImgBtn l_canshu' id='ImagePath1' value=''> <img src="../Images ...
- 真正的让iframe自适应高度 兼容多种浏览器随着窗口大小改变
今天有朋友问到我关于"iframe自适应高度"的问题,原本以为是很简单的问题,没想到折腾了20分钟才搞定.期间遇到几个问题,要么是高度自适应了,但是当窗口改变时会出现滚动条.也就是 ...
- LUA OOP 单例模式实现的 一个 方案
单例 存在这么一类class, 无论class怎么初始化, 产生的instance都是同一个对象. Code string.toHTMLCode = function(self) return enc ...
- RDIFramework.NET 中多表关联查询分页实例
RDIFramework.NET 中多表关联查询分页实例 RDIFramework.NET,基于.NET的快速信息化系统开发.整合框架,给用户和开发者最佳的.Net框架部署方案.该框架以SOA范式作为 ...
- dubbo源码分析3-service bean的创建与发布
dubbo源码分析1-reference bean创建 dubbo源码分析2-reference bean发起服务方法调用 dubbo源码分析3-service bean的创建与发布 dubbo源码分 ...
- Java条件编译
学习过C语言或者C++语言的同学都知道它们支持条件编译,那么今天我们来学习下在Java语言中如何实现条件编译.Java语言本身没有提供条件编译,但是Java编译器对.java文件编译为.class文件 ...