Reading CSS value using JavaScript

前端 未结 7 687
梦毁少年i
梦毁少年i 2021-01-28 17:43

This works:

7条回答
  •  孤独总比滥情好
    2021-01-28 18:19

    I don't think style.width is given a value unless you explicitly define it one. As others have said you can use currentStyle/getComputedStyle to get actual values of CSS items.

    If you need JUST the width you can go with element.offsetWidth. If you need the value as a percent you'll have to compare the elements offsetWidth with the parents offsetWidth.

提交回复
热议问题