JS之获取样式
基本代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
div{
color:yellow;
}
</style>
</head>
<body>
<div style="width:100px;height:100px;background-color:red">This is div</div>
</body>
</html>
1.通过使用element.style属性来获取
<script>
var div = document.getElementsByTagName("div")[0];
console.log(div.style.color); //""
console.log(div.style.backgroundColor); //red
</script>
element.style属性只能获取行内样式,不能获取<style>标签中的样式,也不能获取外部样式
由于element.style是元素的属性,我们可以对属性重新赋值来改写元素的显示。
<script>
var div = document.getElementsByTagName("div")[0];
div.style['background-color'] = "green";
console.log(div.style.backgroundColor); //green
</script>
2.通过getComputedStyle和currentStyle来获取样式
getComputedStyle的使用环境是chrome/safari/firefox IE 9,10,11
<script>
var div = document.getElementsByTagName("div")[0];
var styleObj = window.getComputedStyle(div,null);
console.log(styleObj.backgroundColor); //red
console.log(styleObj.color); //yellow
</script>
currentStyle在IE里能得到完美支持,chrome不支持,ff不支持
<script>
var div = document.getElementsByTagName("div")[0];
var styleObj = div.currentStyle;
console.log(styleObj.backgroundColor); //red
console.log(styleObj.color); //yellow
</script>
3.ele.style和getComputedStyle或者currentStyle的区别
3.1 ele.style是读写的,而getComputedStyle和currentStyle是只读的
3.2 ele.style只能得到行内style属性里面设置的CSS样式,而getComputedStyle和currentStyle还能得到其他的默认值
3.3 ele.style得到的是style属性里的样式,不一定是最终样式,而其他两个得到的是元素的最终CSS样式
4.获取样式兼容性写法
<script>
//获取非行间样式(style标签里的样式或者link css文件里的样式),obj是元素,attr是样式名
function getStyle(obj,attr){
//针对IE
if(obj.currentStyle){
return obj.currentStyle[attr]; //由于函数传过来的attr是字符串,所以得用[]来取值
}else{
//针对非IE
return window.getComputedStyle(obj,false)[attr];
}
}
/*
获取或者设置css属性
*/
function css(obj,attr,value){
if(arguments.length == 2){
return getStyle(obj,attr);
}else{
obj.style[attr] = value;
}
}
</script>
5.window.getComputedStyle(ele[,pseudoElt]);
第二个参数如果是null或者省略,则获取得到是ele的CSSStyleDeclaration对象
如果是一个伪类,则获取到的是伪类的CSSStyleDeclaration对象
<style>
div{
width:200px;
height:200px;
background-color:#FC9;
font-size:20px;
text-align:center;
}
div:after{
content:"This is after";
display:block;
width:100px;
height:100px;
background-color:#F93;
margin:0 auto;
line-height:50px;
}
</style>
<body>
<div id='myDiv'>
This is div
</div>
<input id='btn' type="button" value='getStyle'/>
<script>
var btn = document.querySelector('#btn');
btn.onclick = function(){
var div = document.querySelector('#myDiv');
var styleObj = window.getComputedStyle(div,'after');
console.log(styleObj['width']);
}
</script>
</body>
6.getPropertyValue获取CSSStyleDeclaration对象中的指定属性值
<script>
var div = document.getElementsByTagName("div")[0];
var styleObj = window.getComputedStyle(div,null);
console.log(styleObj.getPropertyValue("background-color"));
</script>
getPropertyValue(propertyName);中的propertyName不能是驼峰式表示
obj.currentStyle['margin-left'] 有效
obj.currentStyle['marginLeft'] 有效
window.getComputedStyle(obj,null)['margin-left'] 有效
window.getComputedStyle(obj,null)['marginLeft'] 有效
window.getComputedStyle(obj,null).getPropertyValue('margin-left') 有效
window.getComputedStyle(obj,null).getPropertyValue('marginLeft') 无效
obj.currentStyle.width 有效
obj.currentStyle.background-color 无效
obj.currentStyle.backgroundColor 有效
window.getComputedStyle(obj,null).width 有效
window.getComputedStyle(obj,null).background-color 无效
window.getComputedStyle(obj,null).backgroundColor 有效
综上,就是带有"-"的属性不能直接点出来,所以有getPropertyValue方法来处理,但是可以用[]来取代getPropertyValue
7.defaultView
在许多在线的演示代码中, getComputedStyle 是通过 document.defaultView 对象来调用的。 大部分情况下,这是不需要的, 因为可以直接通过window对象调用。但有一种情况,你必需要使用 defaultView, 那是在firefox3.6上访问子框架内的样式 (iframe)
MDN--getComputedStyle:https://developer.mozilla.org/zh-CN/docs/Web/API/Window/getComputedStyle
JS之获取样式的更多相关文章
- js如何获取样式?
在某个项目中,我们经常会需要来获取某个元素的样式,比如说获取一个div的color:这样,新的问出现了, var style = box.style.width;console.log(style); ...
- js中获取样式的俩种方法 style.color和style['color'] 区别
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- js中获取css的样式
因为给定一个div宽度或者其他样式之后,再设置一个border的宽度在js中得到的obj.setoffWidth就会变成width加上border的二倍宽度,因此可以自己写一个方法来获取样式.(obj ...
- 原生js获取样式
js中的获取样式是在是让人头疼,为了方便兼容多个浏览器,把设置样式封装成一个函数. 函数如下: function getStyle(element, property) { var value = e ...
- js中获取css样式属性值
关于js中style,currentStyle和getComputedStyle几个注意的地方 (1)用js的style只能获取元素的内联样式,内部样式和外部样式使用style是获取不到的.针对css ...
- 为什么我获取不到这个css样式?js原生获取css样式总结
还是自己遇到的一个坑的总结吧!与其说是坑不如说自己学艺不精,让我先哭一会!! 需求 简单就是获取一个css的height (好吧 就是一个这么简单的需求) 实践 好吧 长时间的JQ 我已经对原生无能了 ...
- JS获取样式
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- style设置/获取样式的问题 和 offsetWidth/offsetHeight的问题
style设置/获取样式的问题:1.js通过style方法 --加样式:加的是行间样式 oDiv.style.width="20"+'px'; --取样式:取得是行间样 ...
- js中获取css属性
直接获取 window.onload = function() { var but = document.getElementById('button'); var div = document.ge ...
随机推荐
- KCF目标跟踪方法分析与总结
KCF目标跟踪方法分析与总结 correlation filter Kernelized correlation filter tracking 读"J. F. Henriques, R. ...
- BZOJ3562 : [SHOI2014]神奇化合物
可以发现,从头到尾有一堆点是始终连在一起的,所以把没被删掉的一开始就有的边都加上后求出每个联通块, 缩完点后我们发现,边数也减少得差不多了,剩下的就直接暴力. #include<cstdio&g ...
- 2分钟 windows下sublime text 3安装git插件:
12:35 2015/11/182分钟 windows下sublime text 3安装git插件:推荐博客:http://blog.csdn.net/naola2001/article/detail ...
- git 设置多项目实现多账号登陆
9:45 2015/11/18git 设置多项目时实现多账号用户登陆git config --global user.name "your_name" git config --g ...
- 匈牙利 算法&模板
匈牙利 算法 一. 算法简介 匈牙利算法是由匈牙利数学家Edmonds于1965年提出.该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最大匹配的算法. 二分图的定义: 设G=(V,E)是一个 ...
- 【noiOJ】p1794
t1794:集合加法 查看 提交 统计 提问 总时间限制: 3000ms 内存限制: 65536kB 描述 给出2个正整数集合A = {pi | 1 <= i <= a},B = {q ...
- Hightcharts设置Y轴最大最小值
有两种方法: 1:是在控件初始化时预设, yAxis: [{ max:}] 2:在程序运行中动态更新设置 thChart.yAxis[].update({ min: , max: });
- C# Textbox的ImeMode取值对中文输入法的影响 (转)
摘自:http://blog.csdn.net/jhycjhyc/article/details/6578570 C# Textbox的ImeMode取值对中文输入法的影响 取值 ...
- Maven进价:Maven的生命周期阶段
一.Maven的生命周期 Maven的生命周期就是对所有的构建过程进行抽象和统一.包含了项目的清理.初始化.编译.测试.打包.集成测试.验证.部署和站点生成等几乎所有的构建步骤. Maven的生命周期 ...
- Odoo Website 替换 Summernote 为第三方富文本编辑器
随着用odoo的人越来越多,奇葩的需求也是越来越多.... 这不,有同学就想替换掉website forum里边的summernote控件,花了点时间研究了一下,先说结论:替换是可行的. 先上替换之后 ...