Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 边框左宽度的getComputedStyle或currentStyle_Javascript_Css - Fatal编程技术网

Javascript 边框左宽度的getComputedStyle或currentStyle

Javascript 边框左宽度的getComputedStyle或currentStyle,javascript,css,Javascript,Css,我的HTML: 我的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).getPropert

我的HTML:

我的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
小提琴:

如何获取
边框左宽度
属性?(在我的示例中,它不工作(在firefox上))

使用jQuery



这是有效的:

检查您的
左边框样式
属性。它被设置为
none
(默认值)。将其设置为类似于
solid
的值,您就可以开始了:

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

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

function getStyle(el, css){
    if(window.getComputedStyle) return getComputedStyle(el, '')[css];
    if(el.currentStyle) return el.currentStyle[css];    
}
警报(getStyle(document.getElementById('bar'),'borderTopWidth')


注意-css定义需要一个样式以及一个边框的宽度来计算宽度(并且它不能设置为显示:当你计算它的尺寸时没有…

…..你的JSFIDLE说“0px”是的,这是正确的。给div一些维度和颜色,你就会明白为什么:JavaScript是正确的。CSS没有做你认为它正在做的事情。您还需要提供左边框样式。是的,它在工作!非常感谢。这是有效的:jsfiddle.net/L2ZwD/1这也是crossbrowser(与ascii lime中的代码不同)在这里表示“未定义”——opera所以不是crossbrowser最好使用jquery(crossbrowser)