javascript - Get actual height of text inside a div - Stack Overflow

So I have a content-editable div that I'm adding text to. I'd like to set up a warning when a

So I have a content-editable div that I'm adding text to. I'd like to set up a warning when a person types a certain amount. However, I have added the ability to change the font size, boldness, italics, etc. So is there a way I can get the actual height of just the text in the div, even if the text hasn't filled the entire div up?

Something like $("#my-div").textHeight() would be awesome

So I have a content-editable div that I'm adding text to. I'd like to set up a warning when a person types a certain amount. However, I have added the ability to change the font size, boldness, italics, etc. So is there a way I can get the actual height of just the text in the div, even if the text hasn't filled the entire div up?

Something like $("#my-div").textHeight() would be awesome

Share Improve this question asked Aug 11, 2014 at 23:58 jsookikijsookiki 5421 gold badge7 silver badges23 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

Or, if you are trying for the height of the entire block of text, try this: fiddle

HTML:

<div id='hold'>
    <div id='text' contenteditable>Lorem ipsum, lorem ipsum</div>
</div>
<button id='checkTextHeight'>Check Text Height</button>

CSS:

#hold {
    border: 1px solid black;
    width: 100px;
    height: 300px;
}
#text {
    min-width: 1em;
    min-height: 1em;
}

JS:

$('#checkTextHeight').click(function() {
    alert($('#text').css('height'));
});

You can encapsulate the content in a display: inline element and take it height by using jquery $('.my-inline-element').height()

Example: http://codepen.io/anon/pen/kbtEr

var size_of_text = $('#my-div').css("font-size");

that should retrieve the font-size of your div, which I'm assuming to be the height of the text.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744276485a4566354.html

相关推荐

  • javascript - Get actual height of text inside a div - Stack Overflow

    So I have a content-editable div that I'm adding text to. I'd like to set up a warning when a

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信