如何解析來自如何獲得邊框寬度jQuery的/ JavaScript的
style="border: solid 1px black;"
邊框寬度的jQuery/JavaScript的?
$elem.css('border-width')
沒有做到這一點。
注意我需要解析來自CSS的寬度,因爲元素可以是display:none
感謝
編輯我沒有實際使用的內嵌樣式,我只是寫它是因爲我沒有意識到存在任何行爲差異。它似乎雖然適用於內聯樣式,但仍然無法從應用的css類中獲取任何值。
如何解析來自如何獲得邊框寬度jQuery的/ JavaScript的
style="border: solid 1px black;"
邊框寬度的jQuery/JavaScript的?
$elem.css('border-width')
沒有做到這一點。
注意我需要解析來自CSS的寬度,因爲元素可以是display:none
感謝
編輯我沒有實際使用的內嵌樣式,我只是寫它是因爲我沒有意識到存在任何行爲差異。它似乎雖然適用於內聯樣式,但仍然無法從應用的css類中獲取任何值。
你可以使用parseInt();
解析邊框寬度從Npx
到N
:
var width = $elem.css('borderWidth'); // 150px
var parsed = parseInt(width); // 150
我有一個谷歌周圍,它出現在爲了得到一個邊框的寬度您必須提供明確的邊界:
var borderWidth = $elem.css("border-left-width");
This i因爲每個邊框都可能有不同的尺寸,樣式和顏色。不幸的是,它似乎並沒有比這更簡單。
jQuery不允許在引用CSS屬性時使用-
,因此請刪除hypen並大寫下面的字母。
$elem.css('borderWidth');
nope,still如果樣式不是內聯的,則無法使其工作。請參閱http://jsfiddle.net/s7YAN/2/ – fearofawhackplanet 2010-09-24 13:54:02
@Ben Everard - jQuery確實允許hyphnes - 它只是不允許使用的簡短CSS - 所以left-border-width可以,但border-width不可以。 – chris 2011-08-27 22:13:07
您的陳述不真實。將小提琴更改爲'border-width',它仍然有效:http://jsfiddle.net/s7YAN/500/ – 2015-03-25 11:05:11
尋求他離開邊界時完成通用的答案,包括有關基數難以捉摸的評論:
<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());
獲取邊框寬度。
示例HTML元素:用上面的HTML
<div>
<style> .example {border: 10px solid lightblue} </style>
<div class="example" style="width: 10px; height: 10px;"></div>
</div>
> $(".example").css("border-width");
< "10px"
稍長非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 jsFiddle(https://jsfiddle.net/user_e/2L8yLc7a/),它演示了我的答案。
如果妳有相等的邊界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
在過去,我通過使用visibility:hidden或在屏幕外設置絕對位置(即left:-10000px)避免了這樣的問題。如果你可以這樣做,一個簡單的'$(elem).outerWidth(false) - $(elem).innerWidth()'會得到你的邊界寬度。 – MikeWyatt 2010-09-24 13:26:30