如何在jQuery / javascript中获取边框宽度

如何parsing边界宽度

style="border: solid 1px black;"

在jQuery / JavaScript?

$elem.css('border-width')

不这样做。

注意我需要parsing从CSS的宽度,因为该元素可能是display:none

谢谢

编辑我没有实际使用内联风格,我只是为了简单而这样写的,因为我没有意识到有任何行为差异。 它似乎虽然工作正常的内联样式,但仍然不能从应用的CSS类获得任何价值。

你可以使用parseInt(); parsing从NpxN的边框宽度:

 var width = $elem.css('borderWidth'); // 150px var parsed = parseInt(width); // 150 

我周围有一个Google,看起来为了获得边框的宽度,你必须提供一个边框的明确的一面:

 var borderWidth = $elem.css("border-left-width"); 

这是因为每个边框都可能有不同的大小,风格和颜色。 不幸的是,它似乎并没有比这更简单。

jQuery不允许使用-当引用一个CSS属性时,请删除这个henn,并大写下面的字母。

 $elem.css('borderWidth'); 

编码在jsFiddle来演示 。

为了在寻找他离开边界时完成Generic的回答,并且包含关于基数的难以捉摸的评论:

 <div style="border-style:solid;border-left-width:15px;"></div> 

在脚本中:

 var width =parseInt($('div').css('borderLeftWidth'),10); alert(width); 
 var bordersOnBothSides = $("#measureMe").outerWidth() - $("#measureMe").innerWidth() ); 

如果你有相同的元素边框widh,或者你需要左边,顶部边框宽度,普通的js:

 elem.clientLeft; //get left border of element elem.clientTop; //get top border of element 

为了得到正确的,底部边框使用jQuery + CSS:

 parseInt($(elem).css('borderLeftWidth'),10); // 10 use to get result in dec, not hex number system 

获取边框宽度。
HTML元素示例:

 <div> <style> .example {border: 10px solid lightblue} </style> <div class="example" style="width: 10px; height: 10px;"></div> </div> 

使用上面的HTML简单的Jquery方式:

 > $(".example").css("border-width"); < "10px" 

另外在search互联网时,我find了一个解决scheme
这工作不使用Jquery JavaScript库
(我认为Jquery可能使用这样的解决scheme):
http://javascript.info/tutorial/styles-and-classes-getcomputedstyle
(JavaScript教程,©Ilya Kantor)

使用上面的HTML稍微长一点的非JQuery的方式:

 > var element = document.getElementById("test"); < undefined > var computed_style = getComputedStyle(element); < CSSStyleDeclaration {0: "animation-delay", 1: "animation-direction", 2: "animation-duration", 3: "animation-fill-mode", 4: "animation-iteration-count", 5: "animation-name", 6: "animation-play-state", 7: "animation-timing-function", 8: "background-attachment", 9: "background-blend-mode", 10: "background-clip", 11: "background-color", 12: "background-image", 13: "background-origin", 14: "background-position", 15: "background-repeat", 16: "background-size", 17: "border-bottom-color", 18: "border-bottom-left-radius", 19: "border-bottom-right-radius", 20: "border-bottom-style", 21: "border-bottom-width", 22: "border-collapse", 23: "border-image-outset", 24: "border-image-repeat", 25: "border-image-slice", 26: "border-image-source", 27: "border-image-width", 28: "border-left-color", 29: "border-left-style", 30: "border-left-width", 31: "border-right-color", 32: "border-right-style", 33: "border-right-width", 34: "border-top-color", 35: "border-top-left-radius", 36: "border-top-right-radius", 37: "border-top-style", 38: "border-top-width", 39: "bottom", 40: "box-shadow", 41: "box-sizing", 42: "caption-side", 43: "clear", 44: "clip", 45: "color", 46: "cursor", 47: "direction", 48: "display", 49: "empty-cells", 50: "float", 51: "font-family", 52: "font-kerning", 53: "font-size", 54: "font-stretch", 55: "font-style", 56: "font-variant", 57: "font-variant-ligatures", 58: "font-weight", 59: "height", 60: "image-rendering", 61: "isolation", 62: "left", 63: "letter-spacing", 64: "line-height", 65: "list-style-image", 66: "list-style-position", 67: "list-style-type", 68: "margin-bottom", 69: "margin-left", 70: "margin-right", 71: "margin-top", 72: "max-height", 73: "max-width", 74: "min-height", 75: "min-width", 76: "mix-blend-mode", 77: "object-fit", 78: "object-position", 79: "opacity", 80: "orphans", 81: "outline-color", 82: "outline-offset", 83: "outline-style", 84: "outline-width", 85: "overflow-wrap", 86: "overflow-x", 87: "overflow-y", 88: "padding-bottom", 89: "padding-left", 90: "padding-right", 91: "padding-top", 92: "page-break-after", 93: "page-break-before", 94: "page-break-inside", 95: "pointer-events", 96: "position", 97: "resize", 98: "right", 99: "speak"…} > computed_style.borderWidth; < "10px" 

完成! 我希望任何人阅读这个理解,也许发现这个有用的。
另外,看到这个jsFiddle ( https://jsfiddle.net/user_e/2L8yLc7a/ ),它演示了我的答案。