How can I access style properties on javascript objects which are using external style sheets?

前端 未结 4 904
长发绾君心
长发绾君心 2021-01-13 10:49

I have an external style sheet with this in it:

.box {
padding-left:30px;
background-color: #BBFF88;
border-width: 0;
overflow: hidden;
width: 400px;
height:         


        
4条回答
  •  囚心锁ツ
    2021-01-13 11:35

    offsetWidth displays the actual width of your div:

    alert(document.getElementById("0").offsetWidth);
    

    This width can be different to what you have set in your css, though. The jQuery way would be (I really don't want to mention them all the time, but that's what all the libraries are there for):

    $("#0").width(); // should return 400
    $("#0").offsetWidth(); // should return 400 as well
    $("#0").css("width"); // returns the string 400px
    

提交回复
热议问题