accumulate everyday:style、currentStyle、getComputedStyle样式
2013-11-14 22:58
471 查看
样式表有三种方式
内嵌样式(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属性里面设置的值。
解决方案:引入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;
}
</styke>
则:
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
内嵌样式(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属性里面设置的值。
解决方案:引入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;
}
</styke>
则:
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
相关文章推荐
- django 正式部署后台样式 丢失问题解决
- css3 demo【0】目录汇总
- css3 demo【1】orman-clarks-vertical-navigation-menu-the-css3-version
- css 学习中小记
- css3之transform-styl 3D旋转效果
- CSS使用方式
- css 中字体大小
- css实现左栏固定右栏自适应,高度自适应的布局
- CSS命名规范和规则
- [css layout][11] three fixed columns
- [css layout][10] three fixed columns
- css 中字体大小
- 图解CSS的padding,margin,border属性(详细介绍及举例说明)
- WIN32控件使用系统样式
- WIN32控件使用系统样式
- Webkit内核探究【2】——Webkit CSS实现
- css布局学习4--inline block
- div+css 实现tabs标签的示例代码
- CSS z-index 属性的使用方法和层级树的概念
- border:none与border:0使用区别