注意: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);
}
本文介绍了一种兼容IE和Firefox浏览器的获取元素样式的JavaScript方法。通过使用mydiv.currentStyle和window.getComputedStyle(mydiv,null)两种方式,可以确保在不同浏览器下正确获取到元素的宽度等样式属性。
309

被折叠的 条评论
为什么被折叠?



