getComputedStyle 或 currentStyle 用于边框左宽

getComputedStyle or currentStyle for border-left-width

本文关键字:边框 用于 currentStyle getComputedStyle      更新时间:2023-09-26

My HTML : <div id="bar" ></div>

我的 CSS :

#bar
{
    border-left-width:150px;
}

我的 JS :

function getStyle(el,styleProp)
{
    if(el.currentStyle)var y=el.currentStyle[styleProp];
    else if(window.getComputedStyle)var y=document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
    return y;
}
alert(getStyle(document.getElementById("bar"),"border-left-width"));//Outputs 0px

小提琴:http://jsfiddle.net/4ABhZ/1

如何获得border-left-width房产?(使用我的示例,它不起作用(在火狐上))

检查您的border-left-style属性。它设置为 none(默认值)。将其设置为类似于solid,您就可以开始了:http://jsfiddle.net/Paulpro/4ABhZ/4/

要支持较旧的浏览器,您需要将带连字符的css更改为camelCase。

您也可以在其他浏览器中使用驼峰,并且直接读取 getComputedStyle 对象的属性。

function getStyle(el, css){
    if(window.getComputedStyle) return getComputedStyle(el, '')[css];
    if(el.currentStyle) return el.currentStyle[css];    
}

alert(getStyle(document.getElementById('bar'),'borderTopWidth'));

注意 - css 定义需要样式和边框的宽度才能具有计算的宽度(并且在计算其尺寸时不能将其设置为显示:none...