最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Selection is changed without throwing the selectionchange event inside nested contenteditable - Stack Overflow

matteradmin4PV0评论

I am writing some sort of editor, where only parts are editable. By clicking behind the row, the caret should be placed at the end of the row for some user input.

In FF this works, in chrome it works until you click behind the row twice. The second click will remove the caret. According to the selection object, the current range is still set to the rows text, but if I for example log the range on "click" it is still the same, but in the view the caret is gone and nothing happens on input.

The following example demonstrates with the visible outline, that the selection seems to change to the wrapping parent, which seems not to be reflected in the pages selection state. How can I detect something that is not there / how does the browser know about the other selection but does not give me access to it? is there any workaround for this problem or am I just stranded in the contenteditable rabithole?

document.addEventListener("selectionchange", () => {
  console.log(document.getSelection().anchorNode?.nodeName)
});
Not Working
<br>
<div contenteditable>
  <div contenteditable="false"><span contenteditable>test</div></span>
  <div contenteditable="false"><span contenteditable>test</div></span>
</div>
<br>
Working
<br>
<div contenteditable>
  <div><span>test</div></span>
  <div><span>test</div></span>
</div>

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far