window.getComputedStyle()方法的使用及其扩展
1.window.getComputedStyle()方法返回值
是一个可以获取当前元素所有最终使用的CSS属性值。返回的是一个CSS样式声明对象([object CSSStyleDeclaration]),只读。
语法:var style = window.getComputedStyle("元素", "伪类"); //第二个参数选填 如果不查询伪类的话可以不填或填null
2.window.getComputedStyle()和element.style的区别
window.getComputedStyle() 和 element.styl 的相同点就是二者返回的都是 CSSStyleDeclaration 对象,取相应属性值得时候都是采用的 CSS 驼峰式写法,均需要注意 float 属性。
而不同点就是:
a.element.style 读取的只是元素的“内联样式”,即 写在元素的 style 属性上的样式;而 getComputedStyle 读取的样式是最终样式,包括了“内联样式”、“嵌入样式”和“外部样式”。
b.element.style 既支持读也支持写,我们通过 element.style 即可改写元素的样式。而 getComputedStyle 仅支持读并不支持写入。
c.我们可以通过使用 getComputedStyle 读取样式,通过 element.style 修改样式
3.兼容性
ie9以下 不支持window.getComputedStyle() 但是可以用element.currentStyle
比如我们想获取元素的高度 (element.currentStyle? element.currentStyle : window.getComputedStyle(element, null)).height;
4.defaultView
在许多在线的演示代码中, getComputedStyle 是通过 document.defaultView 对象来调用的。 大部分情况下,这是不需要的, 因为可以直接通过window对象调用。
但有一种情况,你必需要使用 defaultView, 那是在 Firefox 3.6 上访问子框架内的样式 (iframe).
而且除了在 IE8 浏览器中 document.defaultView === window 返回的是 false 外,其他的浏览器(包括 IE9 )返回的都是 true。所以后面直接使用 window 就好,不用在输入那么长的代码了。
window.getComputedStyle()方法的使用及其扩展的更多相关文章
- getComputedStyle 方法
一:getComputedStyle getComputedStyle是一个可以获取当前元素所有最终使用的CSS属性值.返回的是一个CSS样式声明对象([object CSSStyleDeclarat ...
- 获取元素CSS值之getComputedStyle方法熟悉
by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=2378 一.碎碎念~前 ...
- 利用getComputedStyle方法获取元素css的属性值
在平时的工作中有时候会碰到需要获取元素当前样式的问题,查了一下可以用getComputedStyle这个方法来获取元素计算后的样式(有些我们在css里面没有写的,浏览器默认的样式也可以获得) getC ...
- getComputedStyle方法获取元素CSS值
javascript的style属性只能获取内联样式,对于外部样式和嵌入式样式需要用currentStyle属性.但是,currentStyle在FIrefox和Chrome下不支持,需要用getCo ...
- window上安装 MongoDB 及其 PHP扩展
window上安装 MongoDB 及其 PHP扩展 工具/原料 window MongoDB MongoDB 方法/步骤 MongoDB 下载 MongoDB提供了可用于32位和64 ...
- 网页性能管理详解:浅谈chrome-Timeline及window.requestAnimationFrame()方法
你遇到过性能很差的网页吗? 这种网页响应非常缓慢,占用大量的CPU和内存,浏览起来常常有卡顿,页面的动画效果也不流畅. 你会有什么反应?我猜想,大多数用户会关闭这个页面,改为访问其他网站.作为一个开发 ...
- 转贴:获取元素CSS值之getComputedStyle方法熟悉
获取元素CSS值之getComputedStyle方法熟悉 一.碎碎念~前言 我们都用过jQuery的CSS()方法,其底层运作就应用了getComputedStyle以及getPropertyVal ...
- 关于obj.currentStyle.property、window.getComputedStyle(obj,null).property、obj.style.property的理解
首先是obj,style.property 我一直用这个obj.style.property这个属性来修改内联和外联的obj属性,但是从网上看到了obj.style.property居然只能读取内嵌的 ...
- 原生js使用getComputedStyle方法获取CSS内部属性值
在对网页进行调试的过程中,经常会用到js来获取元素的CSS样式, 1.下面的方法只能JS只能获取写在html标签中的写在style属性中的值(style=”…”),而无法获取定义在<style ...
随机推荐
- 移动端border-radius的几个BUG
个人博客: http://mcchen.club 一.Android 2.3 自带浏览器不支持 % 通常我们实现一个正圆只需要border-radius: 50%即可,大致代码如下 .foo { wi ...
- springmvc处理局部异常和全局异常
springmvc通过HandlerExceptionResolver(是一个接口,在spring-webmvc依赖下)处理程序异常,包括处理器异常.数据绑定异常以及处理器执行时发生的异常.Handl ...
- mac下安装jmeter
jmeter官网下载 双击解压 命令行进入/Users/yanguobin/apache-jmeter-5.1.1/bin目录下,输入sh jmeter即可启动 也可以 配置环境变量 vim ~/.b ...
- springboot结合jpa
idea中新建springboot项目,引入spring-boot-starter-data-jpa依赖 application.yml中配置数据库连接,示例如下: spring: datasourc ...
- c#之线程基础(一)
可以认为线程是一个虚拟进程,用于独立运行一个特定的程序. 1.使用c#创建线程 using System; using System.Threading; //3 namespace MutiThre ...
- Angry Words 愤怒的话
_ Words said in anger are like scars left by nails in a fence. Even though you can pull all the nail ...
- [Mathematics][MIT 18.02]Detailed discussions about 2-D and 3-D integral and their connections
Since it is just a sort of discussion, I will just give the formula and condition without proving th ...
- 一个基于C++11的单例模板类
#ifndef _SINGLETON_H_#define _SINGLETON_H_ template<typename T>class Singleton : public Uncopy ...
- opencv::模糊图像
Smooth/Blur 是图像处理中最简单和常用的操作之一,使用该操作的原因之一就为了给图像预处理时候减低噪声 使用Smooth/Blur操作其背后是数学的卷积计算,通常这些卷积算子计算都是线性操作, ...
- MFC::Visual studio ? 对应VC6-14
VC6VC7: Visual studio.netVC7.1: Visual studio 2003VC8: Visual studio 2005VC9: Visual studio 2008VC10 ...