如何在 jQuery/javascript 中获取边框宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3787502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get border width in jQuery/javascript
提问by fearofawhackplanet
How do I parse the border width from
我如何解析边框宽度
style="border: solid 1px black;"
style="border: solid 1px black;"
in jQuery/javascript?
在 jQuery/javascript 中?
$elem.css('border-width')
$elem.css('border-width')
doesn't do it.
不这样做。
Note I need to parse the width from the css, as the element may be display:none
注意我需要从 css 解析宽度,因为元素可能是 display:none
Thanks
谢谢
EditI'm not actually using an in-line style, I just wrote it that way for simplicity as I didn't realise there was any behavoural difference. It seems to work fine for inline styles though, but still can't get any value from an applied css class.
编辑我实际上并没有使用内联样式,为了简单起见,我只是这样写的,因为我没有意识到有任何行为差异。虽然它似乎适用于内联样式,但仍然无法从应用的 css 类中获得任何值。
回答by djdd87
You can just use parseInt();
to parse the border width from Npx
to N
:
你可以使用parseInt();
从解析边框宽度Npx
到N
:
var width = $elem.css('borderWidth'); // 150px
var parsed = parseInt(width); // 150
var width = $elem.css('borderWidth'); // 150px
var parsed = parseInt(width); // 150
I had a Google around and it appears in order to get the width of a border you must provide an explicit side of the border:
我有一个谷歌,它似乎为了获得边框的宽度,你必须提供边框的明确边:
var borderWidth = $elem.css("border-left-width");
This is because it's possible for every border to have a different size, style and colour. Unfortunately it doesn't appear to be any simpler than this.
这是因为每个边框可能具有不同的大小、样式和颜色。不幸的是,它似乎没有比这更简单的了。
回答by Ben Everard
jQuery doesn't allow the use of -
when referencing a CSS property, so remove the hypen and capitalise the following letter.
jQuery 不允许-
在引用 CSS 属性时使用,因此删除连字符并将以下字母大写。
$elem.css('borderWidth');
回答by jedierikb
var bordersOnBothSides = $("#measureMe").outerWidth() - $("#measureMe").innerWidth() );
回答by Bryan
To complete Generic's answer when seeking he left border and to include elusive's comment about radix:
在寻找他的左边界时完成 Generic 的回答并包括难以捉摸的关于基数的评论:
<div style="border-style:solid;border-left-width:15px;"></div>
in script:
在脚本中:
var width =parseInt($('div').css('borderLeftWidth'),10);
alert(width);
回答by Vasyl Gutnyk
If u have equal border widh for element, or u need left, top border width, plain js:
如果元素的边框宽度相等,或者需要左、上边框宽度、纯 js:
elem.clientLeft; //get left border of element
elem.clientTop; //get top border of element
To get right, bottom border use jquery+css:
为了正确,底部边框使用 jquery+css:
parseInt($(elem).css('borderLeftWidth'),10); // 10 use to get result in dec, not hex number system