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 ...
随机推荐
- Pig Flatten 解包操作,解元组
Flatten Operator The FLATTEN operator looks like a UDF syntactically, but it is actually an operator ...
- 新建用户无法通过SecureSRT进行ssh登录到远程linux (zz)
root新建了一个普通用户oracle,并且设置了密码:通过SecureSRT连接远程linux,连不上[注:用的是之前新建过的以root为用户名的SecureSRT已有连接,如192.168.1.1 ...
- C#识别图中二维码
1.在NuGet中添加 ZXing.Net 2.实例代码 /// <summary> /// 识别图中二维码 /// </summary> /// <param name ...
- Linux6.7 安装图文
Linux6.7 安装图文 选择第一个进行安装 1. Install or upgrade an existing system 安装或升级系统 2. Install system with ...
- Haproxy+Keepalived高可用配置
基本实验 参考文档 博文地址 环境拓扑 下面使我们要实现的负载均衡集群图示 主节点地址: 92.0.0.11 从节点地址: 92.0.0.12 共享虚拟地址:92.0.0.8 下面是负载均衡集群可能出 ...
- 设计模式——懒汉式单例类PK饿汉式单例类
前言 我们都知道生活中好多小软件,有的支持多IP在线,有的仅仅局限于单个IP在线.为什么这样设计,在软件开发阶段就是,有需求就是发展.这就是软件开发的一个设计模式--懒汉式单例类和饿汉式单例类. 内容 ...
- 洛谷P2474 [SCOI2008]天平
P2474 [SCOI2008]天平 题目背景 2008四川NOI省选 题目描述 你有n个砝码,均为1克,2克或者3克.你并不清楚每个砝码的重量,但你知道其中一些砝码重量的大小关系.你把其中两个砝码A ...
- HDU1425 A Chess Game
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1524 思路:题目就是给你一个拓扑图,然后指定点的位置放棋子,然后两人轮流移动棋子(题目中的边的关系),直到 ...
- UIScrollView嵌套滑动手势冲突的简易实现
明确需求 现在有较多的商城类app有如下需求,界面上带有headerView,并且有一个barView可悬停,最下方为多个可左右滑动的tableView,具体可参考下图 另类实现 在网上关于此类需求的 ...
- Proxy模式(代理[延迟]模式)
Proxy?? Proxy是"代理人"的意思,它指的是代替别人进行工作的人.代理实际上就是使用委托的机制,在代理的过程中你可以做点其他的事情,然后再来执行被代理对象的代码. 知识储 ...