代码 代码依旧很简单: 复制代码 代码如下: function getStyle(el,styleProp) { var x = document.getElementById(el); if (x.currentStyle) var y = x.currentStyle[styleProp]; else if (window.getComputedStyle) var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp); return y; }
首先我们传递元素的ID和想访问的样式名称 复制代码 代码如下: function getStyle(el,styleProp) { 然后我们把元素保存在x中: 复制代码 代码如下: var x = document.getElementById(el); 首先是IE的方法:元素的currentStyle属性 复制代码 代码如下: if (x.currentStyle) 2 var y = x.currentStyle[styleProp]; 然后是Mozilla方法:使用getComputeStyle()方法,在Opera也同样可行 [code] else if (window.getComputedStyle) var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);