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 一.碎碎念~前 ...
随机推荐
- MVC应用程序中,怎样控制与复制相同的功能
先看此篇<MVC程序实现Autocomplete功能> http://www.cnblogs.com/insus/p/3546255.html 它是实现使用jQuery实现文本框输入文字, ...
- 关于scrollWidth,clientWidth,offsetWidth
scrollWidth:对象的实际内容的宽度,不包含边线(border)的宽度,会随对象的内容可视区后而变大. scrollHeight:对象的实际内容的高度,不包含边线(border)的宽度,会随对 ...
- 根据value选择select
<script> var tem="{$Zgoods.type_2}"; $("#type_2 option[value='"+tem+" ...
- (转载)移动WEB前端开发资源整合
收藏起来,感谢原文大大:Bon~~~ 原文链接:http://www.ccwebsite.com/development-of-resource-integration-in-mobile-termi ...
- ZeroClipboard 插件实现文本复制到剪贴板
ZeroClipboard 的官网 点这里,github地址 点这里. 事例如下: 在引入 ZeroClipboard.js 之后, <button id="clip_button&q ...
- 响应式布局1--媒体查询和-webkit-min-device-pixel-ratio
-webkit-min-device-pixel-ratio其实就是这个玩意 window.devicePixelRatio是设备上物理像素和设备独立像素(device-independent pix ...
- web前端编写注意点
1.在语义不明显,既可以用 <P> 也可以用 <div> 的地方,尽量用 <P> ,因为 <P> 默认情况下有上下间隔,去样式后的可读性更好,对兼容特殊 ...
- C#文件与流(FileStream、StreamWriter 、StreamReader 、File、FileInfo、Directory、directoryInfo、Path、Encoding)
(FileStream.StreamWriter .StreamReader .File.FileInfo.Directory.DirectoryInfo.Path.Encoding) C#文 ...
- [转]装完CentOS后,重新开机启动后显示: Initial setup of CentOS Linux 7 (core)
转:装完Centos7提示Initial setup of CentOS Linux 7 (core) 在用U盘装完CentOS后,重新开机启动后显示: Initial setup of Cent ...
- 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数012,polygon,多边形
<zw版·Halcon-delphi系列原创教程> Halcon分类函数012,polygon,多边形 为方便阅读,在不影响说明的前提下,笔者对函数进行了简化: :: 用符号“**”,替换 ...