style、currentStyle、getComputedStyle区别介绍
style、currentStyle、getComputedStyle区别介绍
来自:蓝色天空
样式表有三种方式
- 内嵌样式(inline Style) :是写在Tag里面的,内嵌样式只对所有的Tag有效。
- 内部样式(internal Style Sheet):是写在HTML的<head></head>里面的,内部样式只对所在的网页有效。
- 外部样式表(External Style Sheet):如果很多网页需要用到同样的样式(Styles),将样式(Styles)写在一个以.css为后缀的CSS文件里,然后在每个需要用到这些样式(Styles)的网页里引用这个CSS文件。
最常用的是style属性,在JavaScript中,通过document.getElementById(id).style.XXX就可以获取到XXX的值,但意外的是,这样做只能取到通过内嵌方式设置的样式值,即style属性里面设置的值。
style 标准的样式!可能是由style属性指定的!
runtimeStyle 运行时的样式!如果与style的属性重叠,将覆盖style的属性!
currentStyle 指 style 和 runtimeStyle 的结合!
通过currentStyle就可以获取到通过内联或外部引用的CSS样式的值了(仅限IE)
如:document.getElementById("test").currentStyle.top
要兼容FF,就得需要getComputedStyle 出马了
注意:getComputedStyle是firefox中的,
currentStyle是ie中的.
例:
<style>
#mydiv {
width : 300px;
}
</style>
var mydiv = document.getElementById('mydiv');
if(mydiv.currentStyle) {
var width = mydiv.currentStyle['width'];
alert('ie:' + width);
} else if(window.getComputedStyle) {
var width = window.getComputedStyle(mydiv , null)['width'];
alert('firefox:' + width);
}
另外在FF下还可以通过下面的方式获取
document.defaultView.getComputedStyle(mydiv,null).width
window.getComputedStyle(mydiv , null).width
style、currentStyle、getComputedStyle区别介绍的更多相关文章
- javascript 读取内联之外的样式(style、currentStyle、getComputedStyle区别介绍) (转载)
样式表有三种方式: 内嵌样式(inline Style) :是写在Tag里面的,内嵌样式只对所有的Tag有效. (也称作“内联样式”) 内部样式(internal Style Sheet):是写在 ...
- js便签笔记(7)——style、currentStyle、getComputedStyle区别介绍【转载】
转者语: 今天看jQuery源码CSS部分,里面用到了currentStyle和getComputedStyle来获取外部样式. 因为elem.style.width只能获取elem的style属性里 ...
- style currentStyle getComputedStyle的区别和用法
先介绍下层叠样式表的三种形式: 1.内联样式,在html标签中style属性设置. <p style="color:#f90">内联样式</p> 2.嵌入样 ...
- 元素高度、宽度获取 style currentStyle getComputedStyle getBoundingClientRect
1.示例代码 (1)html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- 用JS查看修改CSS样式(cssText,attribute('style'),currentStyle,getComputedStyle)
CSS样式定义方法 大家都知道,在为HTML设置样式的时候,通常有三种方法:内联样式,内部样式表,外部样式表. 1.内联样式: 内联样式表就是在HTML元素中的行内直接添加style属性. <d ...
- 浅谈style.,currentStyle,getComputedStyle,getAttribute
xxx为属性. ele为元素. 1.style.是针对于样式 在前面的一篇博客中我也有说到,ele.style.xxx; 通常用于赋值,赋值也是针对于行内样式,用它来取值的话,它只能取到内联样式. 今 ...
- 关于对象.style currentstyle 的区别
对象.style的方式只能获取行内写法的样式,但是外部引入的或者写在head里面的就无法获取,只能用currentstyle.
- style、currentStyle、getComputedStyle(不同浏览器获取css样式)区别介绍
style.currentStyle.getComputedStyle区别介绍 样式表有三种方式 内嵌样式(inline Style) :是写在Tag里面的,内嵌样式只对所有的Tag有效. 内部样 ...
- obj.style 和currentstyle 等区别
版权声明:本文为博主原创文章,未经博主允许不得转载. 获取样式 obj.style 和currentstyle 等区别 obj.style只能获得内嵌样式(inline Style)就是写 ...
随机推荐
- 【ZZ】编程能力层次模型
http://blog.csdn.net/shendl/article/details/43835421 前言 程序员的编程技能随着经验的积累,会逐步提高.我认为编程能力可以分为一些层次. 下面通过两 ...
- restful php
http://bbs.phpchina.com/thread-228725-1-1.html http://www.cnblogs.com/artech/p/3506553.html http://w ...
- Redis 服务器
Redis 服务器命令主要是用于管理 redis 服务. 实例 以下实例演示了如何获取 redis 服务器的统计信息: redis 127.0.0.1:6379> INFO # Server r ...
- PHP.9-HTML+CSS(三)-CSS样式
CSS样式 CSS是Cascading Style Sheets的简写,它除了可以轻松设置网页元素的显示位置和格式处,甚至还能产生滤镜,图像淡化,网页淡入淡出的渐变效果.CSS就是要对网页的显示效果实 ...
- 使用hibernate自动创建Mysql表失败原因
1.使用 schemaExport(hibernate5.2.3)创建Mysql表 @Test public void testSchemaExport() { ServiceRegistry ser ...
- iOS XMPP(2)自己创建客户端
一.目的以及效果: 用Xcode利用xmpp框架建立客户端,实现向服务器注册添加用户 密码,以及登陆,离线状态 工程的主要结构:新建singleview工程,用xib拖放两个输入框和两个按钮, 并在v ...
- python(4) - 装饰器2
接下来修改一下上一篇的login,将用户名传递给验证函数. def login(func): #接收一个函数作为参数 def inner(name): print("用户验证通过....&q ...
- ganglia监控hadoop2.0配置方法
ganglia监控hadoop2.0配置方法前提:hadoop2.0集群已安装ganglia监控工具第一步:Hadoop用户登录集群每一个节点,修改文件:vi /opt/hadoop-2.0.0-cd ...
- angularjs $state.go 传参
在目标页面规定接受的参数:$stateProvider.state('page2', {params: {'data': null}}) 传参:$state.go('page2', {data: 'a ...
- insert 另外一种用法
then into dept01(id) values(deptno) then into dept02(id) values(deptno) else into dept03(id) values( ...