Get border width from a div with plain javascript

前端 未结 6 2108
日久生厌
日久生厌 2020-12-30 23:44

I got this style applied to a div

div#content {
border: 1px solid skyblue;  
}

and i want to be able to alert the width of the border, I ha

6条回答
  •  渐次进展
    2020-12-31 00:22

    I might be too late but as you never marked it as answered, I thought I could give it a try.

    If your problem was compatibility between browser I would create a custom method that I could use in almost every browser there is (that means going back to the very basics).

    I actually dug a lot to do this. I use some of the code from jQuery because I did not want to use jQuery but still have the backwards compatibility that jQuery does.

    This function solves your question and at the bottom there are some examples on how to use it.

    This functions uses the "module pattern" through the immediate function that will be run as soon as the script loads creating a method that will NOT polute the global scope but extend its functionality through a function to do what you wanted.

    // I give it a name but it can also be anonymous
    (function preloadedFunctions(){
        // Preseted methods.
        if(window.getComputedStyle){
            window.getComputedStylePropertyValue = function(element, prop){
                var computedStyle = window.getComputedStyle(element, null);
                if(!computedStyle) return null;
                if(computedStyle.getPropertyValue) {
                    return computedStyle.getPropertyValue(prop);
                } else if (computedStyle.getAttribute) {
                    return computedStyle.getAttribute(prop);
                } else if(computedStyle[prop]) {
                    return computedStyle[prop];
                };
            };
        }
        // jQuery JavaScript Library v1.9.0
        // http://www.minhacienda.gov.co/portal/pls/portal/PORTAL.wwsbr_imt_services.GenericView?p_docname=6240612.JS&p_type=DOC&p_viewservice=VAHWSTH&p_searchstring=
        // For IE8 or less
        else if ( document.documentElement.currentStyle ) {
            var rnumnonpx = new RegExp( "^(" + core_pnum + ")(?!px)[a-z%]+$", "i" ),
            rposition = /^(top|right|bottom|left)$/,
            core_pnum = /[+-]?(?:\d*\.|)\d+(?:[eE][+-]?\d+|)/.source;
            window.getComputedStylePropertyValue = function(element, prop){
                var left, rsLeft,
                ret = element.currentStyle && element.currentStyle[ prop ],
                style = element.style;
    
                if ( ret == null && style && style[ prop ] ) {
                    ret = style[ prop ];
                }
                if ( rnumnonpx.test( ret ) && !rposition.test( prop ) ) {
                    left = style.left;
                    rsLeft = element.runtimeStyle && element.runtimeStyle.left;
                    if ( rsLeft ) {
                        element.runtimeStyle.left = element.currentStyle.left;
                    }
                    style.left = prop === "fontSize" ? "1em" : ret;
                    ret = style.pixelLeft + "px";
                    style.left = left;
                    if ( rsLeft ) {
                        element.runtimeStyle.left = rsLeft;
                    }
                }
                return ret === "" ? "auto" : ret;
            };
        };
    })();
    

    i.e.

    1.-

    var borderWidth = getComputedStylePropertyValue(document.getElementsByTagName("div")[0], "border-width");
    console.log(borderWidth);
    

    2.-

    var div = document.getElementById("someID");
    console.log(getComputedStylePropertyValue(div, "border-width")); 
    

提交回复
热议问题