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 ...
随机推荐
- stega -- Pcat老入群题
stega -- Pcat老入群题 Pcat师傅的题果然给力,就是看着wp也是琢磨了半天. WP地址:http://mp.weixin.qq.com/s/T9jJLACiZNB6FR226IjmEA ...
- MFC修改对话框标题
对话框标题栏内容为静态 直接在对话框属性"常规"的"Caption"中修改. 动态生成对话框标题栏内容 SetWindowText()函数就可以 CString ...
- android dm-verity 功能【转】
转自:https://blog.csdn.net/ee230/article/details/73348344 Android dm-verity 实现原理深入研究 思维导图: dm-verity 说 ...
- gprof使用介绍【转】
转自:https://blog.csdn.net/linquidx/article/details/5916701 gprof 1.1 简介 gprof实际上只是一个用于读取profile结 ...
- linux系统设置静态IP,DHCP网络服务,DNS
一.设置静态IP及DHCP网络服务 kk@yuanqiangfei:~$ cat /etc/network/interfaces # This file describes the network i ...
- ionic之AngularJS——手势事件
长按 : on-hold 在屏幕同一位置按住超过500ms,将触发on-hold事件: 你可以在任何元素上使用这个指令挂接监听函数: <any on-hold=“…”>…</any& ...
- fpm...failed: Cannot assign requested address
2017年6月23日 16:00:28 星期五 启动fpm的时候总是报错, 看了网上说的修改rlimit值但是不管用 后来发现fpm配置文件监听的地址写错了, 127.0.0.1 写成了 172.0. ...
- 51nod--1242 斐波那契数列第N项 (矩阵乘法优化)
题目: 1242 斐波那契数列的第N项 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 斐波那契数列的定义如下: F(0) = 0 F(1) = 1 F(n) ...
- springboot 文件上传大小配置
转自:https://blog.csdn.net/shi0299/article/details/69525848 springboot上传文件大小的配置有两种,一种是设置在配置文件里只有两行代码,一 ...
- [C]内存管理、内存泄露、堆栈
原文地址:https://www.cnblogs.com/youthshouting/p/4280543.html,转载请注明源地址. 1.内存分配区间: 对于一个C语言程序而言,内存 ...