JavaScript获取样式值的几种方法学习总结
本人经过整理总结出以下获取样式值的方法,如有错误请各位大佬指正。 有四种方法:style,currentStyle,getComputedStyle,rules 与 cssRules方法。
1. style
用法:document.getElementById(“id”).style.property=”值”。
例子:
<style>
.yellow{height:200px;width:200px;background: yellow;}
</style>
</head>
<body>
<div id="div1" class="yellow" style="color:red"></div>
<script>
var div1=document.getElementById("div1");
div1.onclick=long;
function long(){
alert(div1.style.width);//空值
alert(div1.style.color);//red
div1.style.color="blue";
div1.style.width="300px";
alert(div1.style.color);//blue
alert(div1.style.width);//300px
}
</script>
总结:style对象只能读写内联样式的值,不能够读写内部样式和外部样式的值。经过测试兼容IE8,Chrome,Firefox浏览器。
2.currentStyle
用法:document.getElementById(“id”).currentStyle.property。
<style>
.yellow{height:200px;width:200px;background: yellow;}
</style>
</head>
<body>
<div id="div1" class="yellow"></div>
<script>
var div1=document.getElementById("div1");
div1.onclick=long;
function long(){
//div1.currentStyle.width="300px";//Uncaught TypeError: Cannot set property 'width' of undefined
alert(div1.currentStyle.width);//200px
}
</script>
总结:currenStyle只能读取元素最终用到的样式值,不能用来设置相关值。经过测试只对IE8浏览器兼容,Chrome和Firefox,Safari不兼容。
3.getComputedStyle
用法:window.getComputedStyle(元素).property
<style>
.yellow{height:200px;width:200px;background: yellow;}
</style>
</head>
<body>
<div id="div1" class="yellow"></div>
<script>
var div1=document.getElementById("div1");
div1.onclick=long;
function long(){
//window.getComputedStyle(div1).width="300px";// Failed to set the 'width' property on 'CSSStyleDeclaration'
alert(window.getComputedStyle(div1).width);//200px
}
</script>
总结:getComputedStyle只能读取元素最终用到的样式值,不能用来设置相关值。经过测试对Chrome和Firefox,Safari兼容,对IE8浏览器不兼容。
4.rules 与 cssRules方法
用法:document.styleSheets[0].rules[0];
document.styleSheets[0].cssRules[0];
<style>
.yellow{height:200px;width:200px;background: yellow;}
#div1{height:300px;}
</style>
</head>
<body>
<div id="div1" class="yellow"></div>
<script>
var div1=document.getElementById("div1");
div1.onclick=long;
function long(){
if(document.styleSheets[0].rules){//兼容IE8,Chrome,不兼容Firefox
var s1=document.styleSheets[0].rules[0];
var s2=document.styleSheets[0].rules[1];
alert(s1.style.background);//yellow
alert(s2.style.background);//空值
s1.style.background="red";//.yellow中背景色设为红色
s2.style.background="blue";//#div1中背景色设为蓝色,最终根据css就近规则显示蓝色
alert(s1.style.background);//red
alert(s2.style.background);//blue
}else{//兼容Firefox,Chrome,不兼容IE8
var s1=document.styleSheets[0].cssRules[0];
var s2=document.styleSheets[0].cssRules[1];
alert(s1.style.height);//200px
alert(s2.style.height);//300px
s1.style.height="400px";
s2.style.height="600px";
alert(s1.style.height);//400px
alert(s2.style.height);//600px
}
}
</script>
总结:rules和cssRules可以读写内部样式,外部样式的样式值。经过测试Chrome两者都兼容,Firefox兼容cssRules对rules不兼容,IE8浏览器兼容rules对cssRules不兼容。
JavaScript获取样式值的几种方法学习总结的更多相关文章
- Javascript获取value值的三种方法及注意点
JavaScript获取value值,主要有以下三种: 1.用document.getElementById(“id名”).value来获取(例1): 2.通过form表单中的id名或者name名来获 ...
- Extjs获取input值的几种方法
记录一下: ExtJs获取文本框中值的几种方式 EXTHTML 1.Html文本框 如: 获取值的方式为: var tValue = Ext.getDom('test').value; 或者 var ...
- Javascript获取html元素的几种方法
1.通过id获取html元素 <!DOCTYPE html> <html> <head lang="en"> <meta charset= ...
- javascript 获取html元素的三种方法
操作HTML元素 你首先找到该元素. 三种方法来做这件事: 通过id找到HTML元素 通过标签名找到HTML元素 通过类名找到HTML元素 通过id查找HTML元素 在DOM中查找HTML元素的最简单 ...
- Selenium获取input值的两种方法:WebElement.getAttribute("value")和WebElement.getText()
在页面元素的定位中,有时候需要获取到元素的页面显示值,用来作为断言.例如,我需要获取email的值"amy1111@xxx.com". <input class=" ...
- JavaScript获取鼠标位置的三种方法
在一些DOM操作中我们经常会跟元素的位置打交道,鼠标交互式一个经常用到的方面,令人失望的是不同的游览器下会有不同的结果甚至是有的游览器下没结果,这篇文章就鼠标点击位置坐标获取做一些简单的总结. 获取鼠 ...
- [TimLinux] JavaScript 获取元素节点的5种方法
1. getElementById document.getElementById("id_value") # 返回值为Node对象,没有元素时,返回 == undefined值( ...
- 关于JS获取select值的两种实现方法
前几天发了一篇关于javascript获取select值的方法,后来发现有另一种实现方法,所以就都发出来比较一下: 方法一:通过获取option标签的value值来确定: <!DOCTYPE h ...
- JAVA中获取文件MD5值的四种方法
JAVA中获取文件MD5值的四种方法其实都很类似,因为核心都是通过JAVA自带的MessageDigest类来实现.获取文件MD5值主要分为三个步骤,第一步获取文件的byte信息,第二步通过Messa ...
随机推荐
- 5、预测和鉴定miRNA的靶基因
转载:http://www.oebiotech.com/Article/mirnabjyyc.html http://www.ebiotrade.com/newsf/2014-9/2014925941 ...
- 【BZOJ2839】集合计数 组合数+容斥
[BZOJ2839]集合计数 Description 一个有N个元素的集合有2^N个不同子集(包含空集),现在要在这2^N个集合中取出若干集合(至少一个),使得它们的交集的元素个数为K,求取法的方案数 ...
- 1.2 xss原理分析与剖析(3)
0×01 第三方劫持 (外调J/C): 本方法是我看长短短贴代码时知晓的,这篇文章我只是把这个攻击手法整理了出来,来说明这个漏洞,这个攻击手法并不是我发现的,我也不是太清楚是谁.“第三方劫持”就是把资 ...
- Android性能优化系列---管理你的app内存
文章出处:http://developer.android.com/training/articles/memory.html#YourApp Random-access memory(RAM)在任 ...
- jexus处理静态文件(处理后缀)
AspNet_Exts=txt就能把你指定的扩展名交给asp.net处理.同理,可以写很多个,AspNet_Exts=txt,htm,html
- 开源文字识别软件tesseract
1.下载4.0软件,下一步下一步到成功: 2.安装之后配置环境变量,Path中添加安装路径(默认:C:\Program Files (x86)\Tesseract-OCR) 3.新增语言库的环境变量, ...
- MAC电脑下Pycharm新建模板默认添加作者时间等信息
在pycharm使用过程中,对于每次新建文件的shebang行和关于代码编写者的一些个人信息快捷填写,使用模板的方式比较方便. 方法如下: 1.打开pycharm,选择Pycharm-preferen ...
- 基于 bootstrap 字体图标,用纯CSS实现星级评分功能
需要用到的图标 实现原理 关键属性是 text-overflow: clip;,表示直接截断文本.我们经常用这个属性的另一个值 text-overflow: ellipsis; 来做省略表示. 先平铺 ...
- 《OD大数据实战》Spark入门实例
一.环境搭建 1. 编译spark 1.3.0 1)安装apache-maven-3.0.5 2)下载并解压 spark-1.3.0.tgz 3)修改make-distribution.sh VER ...
- matplotlib.pyplot import报错: ValueError: _getfullpathname: embedded null character in path
Environment: Windows 10, Anaconda 3.6 matplotlib 2.0 import matplotlib.pyplot 报错: ValueError: _getfu ...