首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更新内部html后的ContentEditable div -设置游标位置

问更新内部html后的ContentEditable div -设置游标位置
EN

Stack Overflow用户
提问于 2017-04-21 04:28:09
回答 2查看 3.1K关注 0票数 18

我有一个拼写检查解决方案,它使用内容可编辑的div,并在拼写错误的单词周围插入span标记。每次更新div的内部html时,光标都会移动到div的开头。

我知道,如果用户在句子末尾添加新单词(下面的代码),我可以将光标移动到div的末尾。

旧文本:这是一个拼写检查器 新文本:这是一个拼写检查器解决方案

代码语言:javascript
复制
var range = document.createRange();
range.selectNodeContents(element[0]);
range.collapse(false);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);

但是,如果用户在句子中间添加单词,则无法保留光标位置。

旧文本:这是一个拼写检查器。 新文本:这是一个新的拼写检查器

在上述情况下,当光标在"new“后面时,光标会转到div的末尾。

如何保持光标的位置?由于我正在更新html并添加节点,所以在更新之前保存范围并将其添加到选择对象中是无效的。

提前谢谢。

EN

回答 2

Stack Overflow用户

发布于 2019-04-08 16:34:12

据我所知,更改div的内容总是有问题的。

这就是我想出的解决方案。请键入错误单词,如helloo,dudeee

理想情况下,这也适用于textarea。

解决方案详细信息:

  • 使用具有相同文本内容的鬼div。
  • 对幽灵div使用透明颜色。
  • 对幽灵div span文本使用边框-底部。
  • 更改zIndex,使其不出现在

​

代码语言:javascript
复制
// some mock logic to identify spelling error
const errorWords = ["helloo", "dudeee"];

// Find words from string like '   Helloo   world .. '
// Perhaps you could find a better library you that does this logic.
const getWords = (data) =>{
  console.log("Input: ", data);
  const allWords = data.split(/\b/);
  console.log("Output: ", allWords)
  return allWords;
}

// Simple mock logic to identify errors. Now works only for 
// two words [ 'helloo', 'dudeee']
const containsSpellingError = word => {
  const found = errorWords.indexOf(word) !== -1;
  console.log("spell check:", word, found);
  return found;
}

const processSpellCheck = text => {
  const allWords = getWords(text);
  console.log("Words in the string: ", allWords);
  const newContent = allWords.map((word, index) => {
    var text = word;
    if(containsSpellingError(word.toLowerCase())) {
      console.log("Error word found", word);
      text = $("<span />")
        .addClass("spell-error")
        .text(word);
    }
    return text;
  });
  return newContent;
}

function initalizeSpellcheck(editorRef) {
  var editorSize = editorRef.getBoundingClientRect();
  
  var spellcheckContainer = $("<div />", {})
    .addClass("spell-check")
    .prop("spellcheck", "false");
 
  var spellcheckSpan = $("<span />")
    .addClass("spell-check-text-content")
    .css({
      width: editorSize.width,
      height: editorSize.height,
      position: "absolute",
      zIndex: -1
    });
    
  var text = $(editorRef).text();
  
  var newContent = processSpellCheck(text);
  spellcheckSpan.append(newContent);
  
  spellcheckContainer.append(spellcheckSpan);
  spellcheckContainer.insertBefore(editorRef);
  
  $(editorRef).on("input.spellcheck", function(event) {
      var newText = $(event.target).text();
      var newContent = processSpellCheck(newText); 
      $(".spell-check .spell-check-text-content").text("");
      $(".spell-check .spell-check-text-content").append(newContent);
  });
}


$(document).ready(function() {
  var editor = document.querySelector("#editor");
  initalizeSpellcheck(editor);
});
代码语言:javascript
复制
#editor {
  border: 1px solid black;
  height: 200px;
}

.spell-check {
  color: transparent;
}

.spell-error {
  border-bottom: 3px solid orange;
}
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<div id="editor" contenteditable="true" spellcheck="false">
dudeee
</div>

​

票数 6
EN

Stack Overflow用户

发布于 2019-04-08 03:58:09

这个答案可能在SitePoint中有效:

存储选择的x,y:

代码语言:javascript
复制
cursorPos=document.selection.createRange().duplicate();
clickx = cursorPos.getBoundingClientRect().left;
clicky = cursorPos.getBoundingClientRect().top;

恢复所选内容:

代码语言:javascript
复制
cursorPos = document.body.createTextRange();
cursorPos.moveToPoint(clickx, clicky);
cursorPos.select();

SitePoint条款:在contentEditable div中保存/恢复插入符号位置

更新25.10.2019:

上面提到的解决方案不再起作用了,因为所使用的函数是不推荐的。铬支持document.selection吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43533971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档