Force IE contentEditable element to create line breaks on Enter key, without breaking Undo

前端 未结 7 1816
时光取名叫无心
时光取名叫无心 2020-12-05 06:59

On Internet Explorer, a contentEditable DIV creates a new paragraph (

) each time you press Enter whereas Firefox creates a
<
7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-05 07:47

    It may be impossible to get this right. However, it is possible to make the

    tags look like single line breaks, by removing the built-in margin around paragraph tags, using CSS:

    p { margin: 0; }
    

    There is a drawback to this approach: if the user needs to copy/paste text in the contentEditable element, the text may appear to be single-spaced inside the element but double-spaced outside the element.

    Worse, in at least some cases, IE will automatically detect double-line breaks during paste, replacing them with

    tags; this will mess up the formatting of the pasted text.

提交回复
热议问题