contenteditable div loses selection when another input focuses

后端 未结 1 442
清酒与你
清酒与你 2021-01-13 17:59

I have a problem with contenteditable div. When i want to execute a simple command (like bold or italic) on it, i do the following:

  • memorize the div (since it
1条回答
  •  醉梦人生
    2021-01-13 18:40

    If the input and contenteditable element are within the same document, you won't be able to prevent the selection in the contenteditable element from being destroyed. However, what you can do is save the selection before the input box receives focus and restore the selection after the dialog is dismissed.

    Here is some simple example code:

    https://stackoverflow.com/a/3316483/96100

    And here's a fuller example:

    https://stackoverflow.com/a/4690057/96100

    If you place either the input or contenteditable element within a separate iframe, most browsers (although not IE) will preserve the original selection.

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