2010-09-24 91 views
53

如何解析來自如何獲得邊框寬度jQuery的/ JavaScript的

style="border: solid 1px black;"

邊框寬度的jQuery/JavaScript的?

$elem.css('border-width')

沒有做到這一點。

注意我需要解析來自CSS的寬度,因爲元素可以是display:none

感謝

編輯我沒有實際使用的內嵌樣式,我只是寫它是因爲我沒有意識到存在任何行爲差異。它似乎雖然適用於內聯樣式,但仍然無法從應用的css類中獲取任何值。

+3

在過去,我通過使用visibility:hidden或在屏幕外設置絕對位置(即left:-10000px)避免了這樣的問題。如果你可以這樣做,一個簡單的'$(elem).outerWidth(false) - $(elem).innerWidth()'會得到你的邊界寬度。 – MikeWyatt 2010-09-24 13:26:30

回答

87

你可以使用parseInt();解析邊框寬度從NpxN

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

我有一個谷歌周圍,它出現在爲了得到一個邊框的寬度您必須提供明確的邊界:

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

This i因爲每個邊框都可能有不同的尺寸,樣式和顏色。不幸的是,它似乎並沒有比這更簡單。

+23

+1!提示:始終包含基數:'parseInt(width,10)'。缺省值是十六進制。 ''parseInt(「08」)=== 0' and'parseInt(「08」,10)=== 8' – jwueller 2010-09-24 13:30:38

+1

您也可以使用ILMV的jsFiddle示例的這個派生來確認通過簡寫屬性設置的值仍然正確返回: http://jsfiddle.net/s7YAN/1/ – ssokolow 2010-09-24 13:31:27

+0

+1,我不確定他的問題是否在獲取屬性或解析它時遇到問題。 – 2010-09-24 13:33:12

9

jQuery不允許在引用CSS屬性時使用-,因此請刪除hypen並大寫下面的字母。

$elem.css('borderWidth'); 

Coded in jsFiddle to demonstrate

+0

nope,still如果樣式不是內聯的,則無法使其工作。請參閱http://jsfiddle.net/s7YAN/2/ – fearofawhackplanet 2010-09-24 13:54:02

+4

@Ben Everard - jQuery確實允許hyphnes - 它只是不允許使用的簡短CSS - 所以left-border-width可以,但border-width不可以。 – chris 2011-08-27 22:13:07

+1

您的陳述不真實。將小提琴更改爲'border-width',它仍然有效:http://jsfiddle.net/s7YAN/500/ – 2015-03-25 11:05:11

6

尋求他離開邊界時完成通用的答案,包括有關基數難以捉摸的評論:

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

腳本:

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

獲取邊框寬度。
示例HTML元素:用上面的HTML

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


簡單的jQuery方式:

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


也搜索互聯網的時候,我發現了一個解決方案
,沒有使用jQuery工作javascript庫
(我認爲Jquery可能使用類似這樣的解決方案):
http://javascript.info/tutorial/styles-and-classes-getcomputedstyle
(JavaScript的教程,©伊利亞·坎特)

稍長非jQuery的方式使用上述HTML:

> 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" 

完成!我希望任何閱讀這篇文章的人都能理解,也許會發現這很有用
此外,請參閱this jsFiddlehttps://jsfiddle.net/user_e/2L8yLc7a/),它演示了我的答案。

+0

我可能會讓一個jsFiddle更好更直觀地展示我的答案。 – Edward 2015-08-28 21:06:39

+0

jsFiddle展示我的答案:https://jsfiddle.net/user_e/2L8yLc7a/ – Edward 2015-08-29 09:02:43

1

如果妳有相等的邊界widh的元素,或u需要左側,頂部邊框寬度,純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