简体   繁体   English

Javascript:获取内容中的文本可编辑到插入符号

[英]Javascript: Get text in a contenteditable up to the caret

I'm surprised I'm having trouble with this and unable to find an answer.我很惊讶我遇到了这个问题并且无法找到答案。 I'm trying to get the text in a contenteditable, from the start of the contenteditable to the users cursor/caret.我正在尝试从 contenteditable 的开头到用户光标/插入符号获取 contenteditable 中的文本。

Here's a jsFiddle of what I've attempted (click around the contenteditable and watch console.log).这是我尝试过的 jsFiddle (单击 contenteditable 并观看 console.log)。

I get the caret location and then I attempt to get the content:我得到插入符号的位置,然后我尝试获取内容:

I tried using textContent of the contenteditable which works but if there's content like foo<br>bar it outputs foobar when ideally it should output foo\r\nbar (Note: This is for a chrome extension I have no control over the content of the contenteditable).我尝试使用 contenteditable 的textContent ,但如果有像foo\r\nbar foo<br>bar这样的内容,它会在理想情况下输出foobar内容可编辑)。

innerText works as expected outputting foo\r\nbar , but as can be seen in the jsFiddle once the html in contenteditable gets a little complex the caret position doesn't seem to match the location in innerText and I have trouble outputting up to the caret. innerText按预期工作,输出foo\r\nbar ,但正如在jsFiddle中看到的那样,一旦 contenteditable 中的 html 变得有点复杂,插入符号 position 似乎与innerText中的位置不匹配,并且我无法输出到插入符号.

Found some code using the Range interface and modified it to meet my needs in this jsFiddle but had the same problem with <br> as textContent did.使用 Range 接口找到了一些代码,并对其进行了修改以满足我在这个 jsFiddle中的需求,但是<br>textContent有同样的问题。

Note: The user will continue typing as I get the content, so looking for something that doesn't break this flow.注意:当我获得内容时,用户将继续输入,因此寻找不会破坏此流程的内容。

Just looking for direction, any quick tips on what I should try?只是在寻找方向,关于我应该尝试什么的任何快速提示?

In your fiddle I replaced the JavaScript content with:您的小提琴中,我将 JavaScript 内容替换为:

document.querySelector("#edit").addEventListener("click", function(){
  var target = document.querySelector('#edit');
  var sel = document.getSelection();
  if(!sel.toString()) {
    var range = document.getSelection().getRangeAt(0);
    var container = range.startContainer;
    var offset = range.startOffset;
    range.setStart(target, 0);
    //do your stuff with sel.toString()
    console.log(sel.toString());
    //restore the range
    range.setStart(container, offset);
  }
});

Hope this helps.希望这可以帮助。

Edit: since you said编辑:既然你说

Note: The user will continue typing as I get the content, so looking for something that doesn't break this flow.注意:当我获得内容时,用户将继续输入,因此寻找不会破坏此流程的内容。

I thought that the click event was just an example;我以为click事件只是一个例子; getting the text while user is typing implies:在用户键入时获取文本意味着:

  1. the entry point can't be click event but probably a setInterval function入口点不能是click事件,但可能是setInterval function
  2. while user is typing there is no selections, only the caret用户输入时没有选择,只有插入符号

To solve the reported bug is enough changing the code as I did, anyway this is only an example to get the result you are interested in;解决报告的错误就足以像我一样更改代码,无论如何这只是获得您感兴趣的结果的示例; than you have to work on it to achieve the desired behavior for all the possible case of your real scenario.比您必须努力实现真实场景的所有可能情况所需的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM