获取css样式,style、getComputedStyle及currentStyle的区别
样式表有三种:
内嵌样式:<div id="box" style="color:red">box</div>,style写在html中的为内嵌样式;
内联样式:
<style>
#box{
font-size: 25px;
background-color: #ccc;
}
</style>
在html页中直接写入<style></style>的为内联样式;
外部样式:<link rel="stylesheet" href="css/style.css" />这种为外部样式。
现在来测试一个小例子:
<style>
#box{
font-size: 25px;
background-color: #ccc;
}
</style>
<div id="box" style="color:red">box</div>
js代码:
window.onload=function(){
var box=document.getElementById('box');
alert(box.style.color); //弹出red element.style[attr]对内嵌样式有效
alert(box.style.fontSize); //弹出空 从这里可以看出element.style[attr]只对内联样式无效
alert(isStyle(box,'color')); //使用isStyle方法,弹出rgb(255,0,0)
alert(isStyle(box,'fontSize')); //使用isStyle方法,弹出25px
}
/*通过元素节点和属性获取属性值*/
function isStyle(ele,attr){
if(window.getComputedStyle!='undefined'){ //兼容firefox
return window.getComputedStyle(ele,null)[attr];
}else if(ele.currentStyle!='undefined'){ //兼容IE
return ele.currentStyle[attr];
}
}
要想获取内部样式以及外部样式需要通过getComputedStyle和currentStyle来获取,当然也可以获取内嵌样式。
获取css样式,style、getComputedStyle及currentStyle的区别的更多相关文章
- js操作css样式,null和undefined的区别?
1.js操作css的样式 div.style.width="100px"在div标签内我们添加了一个style属性,并设定了width值.这种写法会给标签带来大量的style属性, ...
- js中style,currentStyle和getComputedStyle的区别以及获取css样式操作方法
用js的style只能获取元素的内联样式,内部样式和外部样式使用style是获取不到的. currentStyle可以弥补style的不足(可获取内联样式,内部样式和外部样式),但是只适用于IE. g ...
- js之如何获取css样式
js之如何获取css样式 一.获取内联样式 1 <div id ="myDiv" style="width:100px;height:100px; border ...
- js 设置 获取css样式
先看一段代码,为了体现一会下面说的js用style获取css样式的不同 一:给div设置margin-left(用style设置css样式没什么问题) box.style.marginLeft=&qu ...
- js中获取css样式属性值
关于js中style,currentStyle和getComputedStyle几个注意的地方 (1)用js的style只能获取元素的内联样式,内部样式和外部样式使用style是获取不到的.针对css ...
- 重温JavaScript获取CSS样式的方法(兼容各浏览器)
众所周知,CSS样式有三种类型:行内样式.内部样式和外部样式,JavaScript获取CSS样式时分为两种情况:行内样式获取法 和 非行内样式获取法 . 一.行内样式获取相对简单,通过element. ...
- js获取css样式方法
一.CSS样式共有三种:内联样式(行间样式).内部样式.外部样式(链接式和导入式) <div id="a" style="width: 100px;height: ...
- 为什么我获取不到这个css样式?js原生获取css样式总结
还是自己遇到的一个坑的总结吧!与其说是坑不如说自己学艺不精,让我先哭一会!! 需求 简单就是获取一个css的height (好吧 就是一个这么简单的需求) 实践 好吧 长时间的JQ 我已经对原生无能了 ...
- 每天一个JavaScript实例-展示设置和获取CSS样式设置
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- jQuery获取CSS样式中的颜色值的问题
转自:http://blog.csdn.net/cwj649956781/article/details/23261529 jQuery获取CSS样式中的颜色值的问题,不同浏览器格式不同的解决办法,需 ...
随机推荐
- UISearchBar 自定义处理
首先通过 KVC 获取到内部的 textField, 然后自定制处理 UITextField *searchField = [searchBar valueForKey:@"searchFi ...
- angularJs路由的使用
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Exadata 18.1新特性--云平台存储节点升级
1.传统方式的存储节点升级流程: (1).将存储节点升级包下载到数据库服务器,通常是DB01上. (2).解压缩存储节点升级包. (3).用升级包中的patchmgr工具滚动或非滚动地升级每个存储节点 ...
- Python循环流程
1.for循环 计算1+2+3+……+100的和 count = 0 i = 1 for i in range(101): count+=i print(count) 前n项和公式为:Sn=n*a1+ ...
- python之函数(二)
上一篇中我们在函数体中的写的都是打印语句,用print来输出打印结果.但是在实际使用的时候,我们并不需要将结果输出在控制台上.这时候该如何解决呢? 1. return返回值. 我们可以将函数的结果通过 ...
- Permutation(构造+思维)
A permutation p is an ordered group of numbers p1, p2, ..., pn, consisting of ndistinct positi ...
- CodeForces - 1110E-Magic Stones(差分+思维)
Grigory has nn magic stones, conveniently numbered from 11 to nn. The charge of the ii-th stone is e ...
- Python常用的标准库以及第三方库有哪些?
20个必不可少的Python库也是基本的第三方库 读者您好.今天我将介绍20个属于我常用工具的Python库,我相信你看完之后也会觉得离不开它们.他们是: Requests.Kenneth Reitz ...
- [转] 完全卸载删除gitlab
[From] https://yq.aliyun.com/articles/114619 完全卸载删除gitlab 1.停止gitlab gitlab-ctl stop 2.卸载gitlab(注意这里 ...
- 方格填数--蓝桥杯---dfs
答案:1580 相似题目:N皇后问题 注意要枚举的是什么 #include<iostream> #include<string.h> using namespace std; ...