How to get the height of the text inside of a textarea

后端 未结 8 1612
眼角桃花
眼角桃花 2020-12-05 10:02

I have a textarea with the the text Hello World. I would like to get the height of this text.

I\'ve tried to use:

var eleme         


        
相关标签:
8条回答
  • 2020-12-05 10:46

    In jQuery there is no scrollHeight, so it needs a little workaround. the solution would be:

    var areaheight=$("textarea#element")[0].scrollHeight;
    $("#element").height(areaheight);
    

    or shorter:

    $("#element").height($("#element")[0].scrollHeight)
    
    0 讨论(0)
  • 2020-12-05 10:49

    http://www.quirksmode.org/dom/range_intro.html sorry that I can't be of more help.

    the problem with you example is that inline text does not have a height, it only has a line-height, for it to have a height it needs to be in display block mode, so that all the lines are added to a block of text, even then it all depends on the width of the box and the font-size, font-family etc.

    what ItzWarty suggests is getting the text selection and putting it in a div that has the same font and width as the textarea, which has display block and allows you to get its height.

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