简体   繁体   English

将插入符号位置设置在contenteditable div中的特定位置

[英]Set caret position at a specific position in contenteditable div

SEE BEFORE MARKING DUPLICATE/DOWNVOTING 在标记重复/下降之前看到

  1. The contenteditable div will not have child elements contenteditable div 不会有子元素
  2. I do not want to set the position at the end of the div 我不想在div末尾设置位置
  3. I do not want a cross-browser solution , only Chrome support required 不想要跨浏览器解决方案 ,只需要Chrome支持
  4. Only vanilla JS , no libraries. 只有vanilla JS ,没有库。

I have seen many many solutions. 我见过很多解决方案。 Many by Tim Down, and others. 很多人都是Tim Down和其他人。 But none does work. 但没有一个可行。 I have seen window.getSelection , .addRange etc. but don't see how they apply here. 我见过window.getSelection.addRange等,但是看不到它们在这里是如何应用的。

Here's a jsfiddle . 这是一个jsfiddle

(Tried) Code: (试过)代码:

var node = document.querySelector("div");
node.focus();
var caret = 10; // insert caret after the 10th character say
var range = document.createRange();
range.setStart(node, caret);
range.setEnd(node, caret);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);

You need to position the caret within the text node inside your element, not the element itself. 您需要将插入符号放在元素内的文本节点中,而不是元素本身。 Assuming your HTML looks something like <div contenteditable="true">Some text</div> , using the firstChild property of the element will get the text node. 假设您的HTML看起来像<div contenteditable="true">Some text</div> ,使用元素的firstChild属性将获得文本节点。

Updated jsFiddle: 更新了jsFiddle:

http://jsfiddle.net/xgz6L/8/ http://jsfiddle.net/xgz6L/8/

Code: 码:

var node = document.querySelector("div");
node.focus();
var textNode = node.firstChild;
var caret = 10; // insert caret after the 10th character say
var range = document.createRange();
range.setStart(textNode, caret);
range.setEnd(textNode, caret);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);

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

相关问题 在contenteditable div中设置插入符位置 - Set caret position in contenteditable div 如何在特定的contenteditable div上设置插入符/光标位置? - How do I set the caret/cursor position on a specific contenteditable div? 在有孩子的 'contenteditable' div 中设置插入符号 Position - Set Caret Position in 'contenteditable' div that has children 修复 contenteditable div 上的插入符号位置 - Fix caret position on contenteditable div 如何在 contenteditable 元素(div)中设置插入符号(光标)position? - How to set the caret (cursor) position in a contenteditable element (div)? 在Chrome / webkit中的contenteditable div图层中设置插入位置 - Set caret position in contenteditable div layer in Chrome/webkit 在contentEditable div中的inserted元素后面设置插入位置 - Set caret position right after the inserted element in a contentEditable div 如何在Firefox和旧版IE中的contenteditable div中设置插入符位置 - How to set the caret position in a contenteditable div in Firefox and old IE versions 替换 contenteditable div 中的单词并正确设置插入符号位置 - Replace word in contenteditable div and properly set caret position h如何获得 contentEditable div 的插入符号位置? - hHow to get caret position for contentEditable div?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM