How can i get default font size in pixels by using

2019-03-13 03:19发布

As you know em is a relative font measurement where one em is equal to the height of the letter "M" in the default font size. An advantage in using it is because you will be able to resize the text.

But how can i get default font size of current environment (in pixels) by using JavaScript or JQuery ?

regards,

8条回答
Ridiculous、
2楼-- · 2019-03-13 04:17

Using jQuery (assuming that you want the font size of a specific element):

var originalFontSize = $('#your_element_id_here').css('font-size');

If you're using Prototype as well as jQuery, you'll want to use the jQuery prefix instead of the dollar sign:

var originalFontSize = jQuery('#your_element_id_here').css('font-size');

This will return the value as a string like so: 16px.

查看更多
【Aperson】
3楼-- · 2019-03-13 04:17

This works in FF.

<script>
function getStyle(el,styleProp)
{
    var x = document.getElementById(el);
    if (x.currentStyle)
        var y = x.currentStyle[styleProp];
    else if (window.getComputedStyle)
        var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
    return y;
}

function GetSt()
{
    var font_size = getStyle("div2","fontSize"); 
    alert ( font_size );
}

</script>
<div id="div1" style="height: 100px;font-size: 20px;">
<div id="div2" style="font-size: 2em;">
test
</div>
</div>
<button onclick="GetSt();">Click</button>
查看更多
登录 后发表回答