style、currentStyle、getComputedStyle区别介绍
来源:互联网 发布:怎么复制淘宝商品链接 编辑:程序博客网 时间:2024/06/05 02:44
内嵌样式(inline Style) :是写在Tag里面的,内嵌样式只对所有的Tag有效。
内部样式(internal Style Sheet):是写在HTML的里面的,内部样式只对所在的网页有效。
外部样式表(External Style Sheet):如果很多网页需要用到同样的样式(Styles),将样式(Styles)写在一个以.css为后缀的CSS文件里,然后在每个需要用到这些样式(Styles)的网页里引用这个CSS文件。 最常用的是style属性,在JavaScript中,通过document.getElementById(id).style.XXX就可以获取到XXX的值,但意外的是,这样做只能取到通过内嵌方式设置的样式值,即style属性里面设置的值。
解决方案:引入currentStyle,runtimeStyle,getComputedStyle 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;
<pre name="code" class="html">CSS代码:.button { height: 2em; border: 0; border-radius: .2em; background-color: #34538b; color: #fff; font-size: 12px; font-weight: bold;}HTML代码:<input type="button" id="button" class="button" value="点击我,显示我" /><p id="detail"></p>JS代码:var oButton = document.getElementById("button"), oDetail = document.getElementById("detail");if (oButton && oDetail) { oButton.onclick = function() { var oStyle = this.currentStyle? this.currentStyle : window.getComputedStyle(this, false); var key, html = '本按钮CSS属性名和属性值依次为('+ !!this.currentStyle +'):'; if (typeof oStyle === "object") { for (key in oStyle) { if (/^[a-z]/i.test(key) && oStyle[key]) { html = html + '' + key + ":" + oStyle[key] + ''; } } } else { html += '无法获取CSS样式!'; } oDetail.innerHTML = html; };}
摘自:http://www.cnblogs.com/flyjs/archive/2012/02/20/2360502.html
http://www.zhangxinxu.com/study/201205/currentstyle-getcomputedstyle-test.html
- style、currentStyle、getComputedStyle区别介绍
- style、currentStyle、getComputedStyle区别介绍
- style、currentStyle、getComputedStyle区别介绍
- style、currentStyle、getComputedStyle区别介绍
- style、currentStyle、getComputedStyle区别介绍
- style、currentStyle、getComputedStyle区别介绍
- style、currentStyle、getComputedStyle区别介绍
- style、currentStyle、getComputedStyle区别介绍
- style、currentStyle、getComputedStyle区别介绍
- style、currentStyle、getComputedStyle区别介绍
- JavaScript强化教程——style、currentStyle、getComputedStyle区别介绍
- JavaScript强化教程——style、currentStyle、getComputedStyle区别介绍
- Js中的style,currentStyle,getComputedStyle()区别
- style,currentStyle,getComputedStyle的区别和用法
- JS中style、currentStyle、getComputedStyle的区别
- JS中style、currentStyle、getComputedStyle的区别
- style currentStyle getComputedStyle的区别和用法
- style currentStyle getComputedStyle
- 朋友范LOGO故事
- 封装Linux下网络连通性检测函数
- 线程函数 pthread_detach()
- 一个数值计算中通常很有用的数值的定义和计算
- Cocoa Touch事件处理流程--响应者链
- style、currentStyle、getComputedStyle区别介绍
- LeetCode 之 Search for a Range — C++ 实现
- XCode Add Exception Breakpoint 与 nan错误的解决方法
- maven项目在eclipse debug到本地tomcat丢包问题
- 什么时候面向文档NoSQL数据库可以代替SQL数据库
- 【HTML+CSS】(2)CSS Sprite雪碧图
- 六月Delphi XE8 及 C++Builder XE8 中文手册如期上市
- loadrunner11 下载路径+安装+破解+汉化
- Material Design