Getting the width of an html element in percent % with jQuery

前端 未结 3 965
孤独总比滥情好
孤独总比滥情好 2021-01-27 06:27

If I alert the element which has a css selector set to width:100% I get it\'s with in px, is there some way to get it in % as

3条回答
  •  情话喂你
    2021-01-27 06:44

    function percentwidth(elem){
        var pa= elem.offsetParent || elem;
        return ((elem.offsetWidth/pa.offsetWidth)*100).toFixed(2)+'%';
    }
    

提交回复
热议问题