星空网 > 软件开发 > Java

javascript 读取内联之外的样式(style、currentStyle、getComputedStyle区别介绍) (转载)

 

样式表有三种方式:

内嵌样式(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>



则:

javascript 读取内联之外的样式(style、currentStyle、getComputedStyle区别介绍) (转载)
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);}
javascript 读取内联之外的样式(style、currentStyle、getComputedStyle区别介绍) (转载)



另外在FF下还可以通过下面的方式获取

document.defaultView.getComputedStyle(mydiv,null).width;window.getComputedStyle(mydiv , null).width;

备注: null参数设置:after,:before之类的伪类,如果不用伪类的话设置为null即可






原标题:javascript 读取内联之外的样式(style、currentStyle、getComputedStyle区别介绍) (转载)

关键词:JavaScript

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

倍特国际货运:https://www.goluckyvip.com/tag/101077.html
白酒怎么寄国际物流:https://www.goluckyvip.com/tag/101078.html
国际货运企业:https://www.goluckyvip.com/tag/101079.html
跨境必:https://www.goluckyvip.com/tag/10108.html
韩国ems国际快递:https://www.goluckyvip.com/tag/101080.html
韩国国际物流价格:https://www.goluckyvip.com/tag/101081.html
DTC个护品牌Svish Ropes完成新一轮融资:https://www.kjdsnews.com/a/1842223.html
五一节三亚、南山寺,海棠湾,海角天崖的门票各是多少,我是外地人?:https://www.vstour.cn/a/409234.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流