Get true content size

心不动则不痛 提交于 2019-12-08 02:54:17

问题


I am wondering if its possible to get the size of the content ( not the div ) in javascript/jQuery. So if the box has a height of 200px but there is only one line of text, the content size would be around 12px.

Thanks for all your help


回答1:


You may create a range of the element's text-contents and use the properties of the range:

/**
  * @param object o DOMElement
  * @return int|null boundingHeight
  **/
function fx(o)
{
  if(document.createRange)
  {
    var r = document.createRange();
        r.selectNodeContents(o);
    return r.getBoundingClientRect().height;
  }
  else
  {
    var r = document.body.createTextRange();
        r.moveToElementText(o)
    return r.boundingHeight;
  }
  return null;
}

http://jsfiddle.net/doktormolle/wArMN/




回答2:


If you are referring the font-size, you can check it with this:

$('div').click(function()
{
    alert($(this).css('font-size'));
});

Demo.



来源:https://stackoverflow.com/questions/6325561/get-true-content-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!