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

前端 未结 3 958
孤独总比滥情好
孤独总比滥情好 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:37

    You can get the width in percentage by two methods.

    Live Demo

    With jQuery use jQuery 1.3.2 and you will get 100%.

    alert($('.my-element').css('width'))​;
    

    With javascript by setting width in style attribute of the tag like this

    <div id="div2" style="width:100%"></div>
    
    div2 = document.getElementById('div2');
    
    alert("Width of div2 with style = " + div2.style.width);
    
    0 讨论(0)
  • 2021-01-27 06:44
    function percentwidth(elem){
        var pa= elem.offsetParent || elem;
        return ((elem.offsetWidth/pa.offsetWidth)*100).toFixed(2)+'%';
    }
    
    0 讨论(0)
  • 2021-01-27 06:49

    This post

    Is it possible to use jQuery to get the width of an element in percent or pixels, based on what the developer specified with CSS?

    0 讨论(0)
提交回复
热议问题