getComputedStyle()用法详解
那如果元素即没有在style属性中设置宽高,也没有在样式表中设置宽高,还能用getComputedStyle或currentStyle获取吗?答案是getComputedStyle可以,currentStyle不可以 。
实际项目中,可能要获取元素的CSS样式属性值,然后再进行其他的操作。
在操作中可能会遇到这种情况,有时候能够正确获取CSS属性值,有时候则不能。
下面通过代码实例分析一下出现这种问题的原因,以及解决方案。
首先看一段代码实例:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>getComputedStyle使用</title>
<style type="text/css">
div{
width:100px;
height:100px;
margin-top:20px;
}
#first{background-color:red}
</style>
<script type="text/javascript">
window.onload=function(){
var first=document.getElementById("first");
var second=document.getElementById("second");
alert(first.style.backgroundColor);
alert(second.style.backgroundColor);
}
</script>
</head>
<body>
<div id="first"></div>
<div id="second" style="background-color:blue"></div>
</body>
</html>
代码运行后,顺利弹出第二个div的背景颜色,第一个的没有获取到。
不少初学者一开始可能人为dom元素对象的style属性无所不能,不但能设置元素的样式,也能够获取到对应的样式,但它不是万能的,它只能够获取通过style设置的元素CSS属性值:
(1).style内签到HTML标签内设置。
(2).dom.style.width="100px"这样类似设置。
这时getComputedStyle方法的作用就得以体现,它可以获取指定元素对应CSS属性的最终计算值。
语法结构:
window.getComputedStyle(element, [pseudoElt])
参数解析:
(1).element:必需,要获取样式值的元素对象。
(2).pseudoElt:可选,表示指定节点的伪元素(:before、:after、:first-line、:first-letter等)。
浏览器支持:
(1).IE9+浏览器支持此方法。
(2).edge浏览器支持此方法。
(3).谷歌浏览器支持此方法。
(4).opera浏览器支持此方法。
(5).火狐浏览器支持此方法。
(6).safria浏览器支持此方法。
代码实例:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>getComputedStyle使用</title>
<style type="text/css">
div{
width:100px;
height:100px;
margin-top:20px;
}
#first{background-color:red}
</style>
<script type="text/javascript">
window.onload=function(){
var first=document.getElementById("first");
var second=document.getElementById("second");
alert(window.getComputedStyle(first,null).backgroundColor);
alert(second.style.backgroundColor);
}
</script>
</head>
<body>
<div id="first"></div>
<div id="second" style="background-color:blue"></div>
</body>
</html>
以上代码成功的获取了第一个div的背景颜色。
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>getComputedStyle使用</title>
<style type="text/css">
div{
width:100px;
height:100px;
margin-top:20px;
}
#first{background-color:red}
#first::before{
content:"添加的内容";
color:#0000ff;
}
</style>
<script type="text/javascript">
window.onload=function(){
var first=document.getElementById("first");
var second=document.getElementById("second");
alert(window.getComputedStyle(first,":before").color);
}
</script>
</head>
<body>
<div id="first"></div>
<div id="second" style="background-color:blue"></div>
</body>
</html>
以上代码可以获取伪元素中字体颜色值(颜色会被转换成RGB或者RGBA模式)。
不能直接获取复合属性值,例如padding属性的值,只能读paddingLeft、paddingTop等属性。
getComputedStyle()用法详解的更多相关文章
- C#中string.format用法详解
C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...
- @RequestMapping 用法详解之地址映射
@RequestMapping 用法详解之地址映射 引言: 前段时间项目中用到了RESTful模式来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没 ...
- linux管道命令grep命令参数及用法详解---附使用案例|grep
功能说明:查找文件里符合条件的字符串. 语 法:grep [-abcEFGhHilLnqrsvVwxy][-A<显示列数>][-B<显示列数>][-C<显示列数>] ...
- mysql中event的用法详解
一.基本概念mysql5.1版本开始引进event概念.event既“时间触发器”,与triggers的事件触发不同,event类似与linux crontab计划任务,用于时间触发.通过单独或调用存 ...
- CSS中伪类及伪元素用法详解
CSS中伪类及伪元素用法详解 伪类的分类及作用: 注:该表引自W3School教程 伪元素的分类及作用: 接下来让博主通过一些生动的实例(之前的作业或小作品)来说明几种常用伪类的用法和效果,其他的 ...
- c++中vector的用法详解
c++中vector的用法详解 vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于一个动态的数组,当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间 ...
- AngularJS select中ngOptions用法详解
AngularJS select中ngOptions用法详解 一.用法 ngOption针对不同类型的数据源有不同的用法,主要体现在数组和对象上. 数组: label for value in a ...
- systemctl命令用法详解
systemctl命令用法详解系统环境:Fedora 16binpath:/bin/systemctlpackage:systemd-units systemctl enable httpd.serv ...
- CSS3的@keyframes用法详解:
CSS3的@keyframes用法详解:此属性与animation属性是密切相关的,关于animation属性可以参阅CSS3的animation属性用法详解一章节. 一.基本知识:keyframes ...
随机推荐
- ANSI C、ISO C、Standard C联系与区别
做C语言开发的人,经常会遇到“ANSI C”.“ISO C”与“Standard C”三种术语,经常会让人傻傻分不清楚.博主之前按也是搞不清三者的关系,于是某天下定决心,一定要搞清楚三者的关系,先百度 ...
- L3-2 森森快递 (30 分)(贪心+线段树/分块)
题目链接:https://pintia.cn/problem-sets/1108203702759940096/problems/1108204121661857798 题目大意: 森森开了一家快递公 ...
- django drf 基础学习1
一 环境配置 python3.5+ django2.0 pymysql二 安装 /usr/bin/python3 -m pip install django /usr/bin/pytho ...
- MySQL触发器trigger的使用
https://www.cnblogs.com/geaozhang/p/6819648.html 触发器的触发 语句的错误 和 触发器里面 错误 都不会运行 NEW与OLD详解 MySQL 中定义了 ...
- 2017-2018-2 20165221实验二《Java面向对象程序设计》实验报告
JAVA实验二报告 课程:Java程序设计 姓名:谭笑 学号:20165221 实验时间:2018.4.13--2018.4.15 实验2--1 实验内容 实现百分制成绩转成"优.良.中.及 ...
- Python内建函数-callable
Python内建函数-callable callable(object) 中文说明:检查对象object是否可调用.如果返回True,object仍然可能调用失败:但如果返回False,调用对象ojb ...
- python中常用模块
OS模块 python编程时,经常和文件.目录打交道,这是就离不了os模块.os模块包含普遍的操作系统功能,与具体的平台无关os.name()——判断现在正在实用的平台,Windows 返回 ‘nt' ...
- 题解-PKUWC2018 随机游走
Problem loj2542 题意:一棵 \(n\) 个结点的树,从点 \(x\) 出发,每次等概率随机选择一条与所在点相邻的边走过去,询问走完一个集合 \(S\)的期望时间,多组询问 \(n\le ...
- EF 常见异常总结
问题:System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invoca ...
- windows下java环境变量的配置 javac不是内部或外部命令的问题
安装配置JAVA JDK 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html . 下载你电脑对应的JDK,下 ...