How to make a content editable div behave like a text area?

后端 未结 2 1058
情歌与酒
情歌与酒 2021-02-06 07:48

I have built an editor that converts markdown to html. Right now I have to use jquery autosize plugin to resize the text area as it grows.

If I use a content-editable di

2条回答
  •  孤城傲影
    2021-02-06 08:24

    After searching for an answer and not finding anything that worked completely I wrote my own jQuery plugin.

    https://github.com/UziTech/jquery.toTextarea.js

    I used white-space: pre-wrap; and inserted '\n' on enter. That way I can use $("div").text() to get the text and not worry about removing tags and formatting
    's

    DEMO:

    http://jsfiddle.net/UziTech/4msdgjox/

提交回复
热议问题