简体   繁体   中英

I can't get a Div's width using element.style.width. Do I have to use element.clientWidth instead?

I think I'm going mad!

I'm starting to write a little exercise for myself where I am going to have some divs that I can drag on the rightborder to increase or decrease the Div width. I also have a container Div that has a set width and I'm going to use this to determine a percentage - basically I'm going to be making some kind of bar-chart / histogram that you can edit.

I'm started writing my code and I thought I'd just make sure I could output the percentage.

Here's the perliminary code....

<style>
#container{width:500px;}
#dragDiv{border:1px solid #000;background-color:pink;width:100px;height:100px;}
</style>
</head>

<body>

<div id="container">
    <div id="dragDiv"></div>
</div>

<script>

    function dragOneSide(innDiv, outDiv){

        if(document.getElementById(innDiv) && document.getElementById(outDiv)){

        var iDiv = document.getElementById(innDiv),
            oDiv = document.getElementById(outDiv);

            // write out the width as a percentage
            var iDivWidth = parseInt(iDiv.style.width),
                oDivWidth = parseInt(oDiv.style.width);
                //alert(document.getElementById("dragDiv").style.width);
                iDiv.innerHTML = ((iDivWidth / oDivWidth) * 100);
        }   
    }

    window.onload = function(){
        dragOneSide("dragDiv", "container");
    }

</script>

Now the value in the iDiv was NaN? I found that rather odd. When trying to alert width I was getting a blank, literally an empty string! Rather odd I thought, especially as I wasn't trying to do anything complicated. I used firebug, set a breakpoint and observed the watch window. There was no value for the Div's width. I then put an inline style on the DIV like so...

<div id="container">
    <div id="dragDiv" style="width:300px;">Hello World</div>
</div>

and low and behold I was now getting a value for the item! Now I don't like inline styles (who does) however I've never had this problem before and I've been using JavaScript and HTML for years - has anyone got an explaination for this? To retrieve the width not set by CSS do I have to use a different property like clientWidth ?

Ps. I haven't included any of the dragging code yet so please don't point that out.

The call to style.width retrieves the style value, which isn't set.

http://jsfiddle.net/EC2HR/

See this example. Yes, you want to use clientWidth in this case.

The simplest way is to use offsetWidth property:

var iDivWidth = parseInt(iDiv.style.offsetWidth),
    oDivWidth = parseInt(oDiv.style.offsetWidth);

style.width is a DOM api which returns the width of an element when it's set inline or via the element.style.width = n + "px"; So that it reacts the way you describe it is as designed.

The offsetWidth like ioseb refers to is a DOM api call which returns the amount of horizontal space an element takes up.

Beware of the many inconsistencies between browsers .

PM5544...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM