How to get border width in jQuery/javascript

后端 未结 5 582
南方客
南方客 2020-11-30 07:12

How do I parse the border width from

style=\"border: solid 1px black;\"

in jQuery/javascript?

$elem.css(\'border-width\')<

相关标签:
5条回答
  • 2020-11-30 07:38

    If u have equal border widh for element, or u need left, top border width, plain js:

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

    To get right, bottom border use jquery+css:

    parseInt($(elem).css('borderLeftWidth'),10); // 10 use to get result in dec, not hex number system
    
    0 讨论(0)
  • 2020-11-30 07:50

    You can just use parseInt(); to parse the border width from Npx to N:

    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.

    0 讨论(0)
  • 2020-11-30 07:59
    var bordersOnBothSides = $("#measureMe").outerWidth() - $("#measureMe").innerWidth() );
    
    0 讨论(0)
  • 2020-11-30 08:00

    jQuery doesn't allow the use of - when referencing a CSS property, so remove the hypen and capitalise the following letter.

    $elem.css('borderWidth');
    

    Coded in jsFiddle to demonstrate.

    0 讨论(0)
  • 2020-11-30 08:00

    To complete Generic's answer when seeking he left border and to include elusive's comment about radix:

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

    in script:

    var width =parseInt($('div').css('borderLeftWidth'),10);
    alert(width);
    
    0 讨论(0)
提交回复
热议问题