我如何修改这个(How to set caret(cursor) position in contenteditable element (div)?)以便接受数字索引和元素并将光标位置设置为该索引?
例如: 如果我有段落:
<p contenteditable="true">This is a paragraph.</p>
我打来电话:
setCaret($(this).get(0), 3)
光标将移动到索引3,如下所示:
Thi|s is a paragraph.
我有这个,但没有运气:
function setCaret(contentEditableElement, index)
{
var range,selection;
if(document.createRange)//Firefox, Chrome, Opera, Safari, IE 9+
{
range = document.createRange();//Create a range (a range is a like the selection but invisible)
range.setStart(contentEditableElement,index);
range.collapse(true);
selection = window.getSelection();//get the selection object (allows you to change selection)
selection.removeAllRanges();//remove any selections already made
selection.addRange(range);//make the range you have just created the visible selection
}
else if(document.selection)//IE 8 and lower
{
range = document.body.createTextRange();//Create a range (a range is a like the selection but invisible)
range.moveToElementText(contentEditableElement);//Select the entire contents of the element with the range
range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start
range.select();//Select the range (make it the visible selection
}
}
答案 0 :(得分:9)
这是一个改编自Persisting the changes of range objects after selection in HTML的答案。请记住,这在几个方面并不完美(正如MaxArt的,使用相同的方法):首先,只考虑文本节点,这意味着<br>
隐含的换行符和块元素不是包括在索引中;其次,考虑所有文本节点,甚至是那些被CSS或<script>
元素内部隐藏的元素内部;第三,页面上折叠的连续空白字符都包含在索引中;最后,IE&lt; = 8的规则再次不同,因为它使用不同的机制。
var setSelectionByCharacterOffsets = null;
if (window.getSelection && document.createRange) {
setSelectionByCharacterOffsets = function(containerEl, start, end) {
var charIndex = 0, range = document.createRange();
range.setStart(containerEl, 0);
range.collapse(true);
var nodeStack = [containerEl], node, foundStart = false, stop = false;
while (!stop && (node = nodeStack.pop())) {
if (node.nodeType == 3) {
var nextCharIndex = charIndex + node.length;
if (!foundStart && start >= charIndex && start <= nextCharIndex) {
range.setStart(node, start - charIndex);
foundStart = true;
}
if (foundStart && end >= charIndex && end <= nextCharIndex) {
range.setEnd(node, end - charIndex);
stop = true;
}
charIndex = nextCharIndex;
} else {
var i = node.childNodes.length;
while (i--) {
nodeStack.push(node.childNodes[i]);
}
}
}
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}
} else if (document.selection) {
setSelectionByCharacterOffsets = function(containerEl, start, end) {
var textRange = document.body.createTextRange();
textRange.moveToElementText(containerEl);
textRange.collapse(true);
textRange.moveEnd("character", end);
textRange.moveStart("character", start);
textRange.select();
};
}
答案 1 :(得分:7)
range.setStart
和range.setEnd
可用于 text 节点,而非元素节点。否则他们会引发DOM异常。所以你需要做的是
range.setStart(contentEditableElement.firstChild, index);
我不知道你为IE8做了什么,并且降低了。你在哪里使用index
?
总的来说,如果节点的内容超过单个文本节点,则代码将失败。具有isContentEditable === true
的节点可能会发生这种情况,因为用户可以从Word或其他位置粘贴文本,或者创建新行等等。
以下是我在框架中所做的改编:
var setSelectionRange = function(element, start, end) {
var rng = document.createRange(),
sel = getSelection(),
n, o = 0,
tw = document.createTreeWalker(element, NodeFilter.SHOW_TEXT, null, null);
while (n = tw.nextNode()) {
o += n.nodeValue.length;
if (o > start) {
rng.setStart(n, n.nodeValue.length + start - o);
start = Infinity;
}
if (o >= end) {
rng.setEnd(n, n.nodeValue.length + end - o);
break;
}
}
sel.removeAllRanges();
sel.addRange(rng);
};
var setCaret = function(element, index) {
setSelectionRange(element, index, index);
};
这里的诀窍是使用setSelectionRange
函数 - 用start === end
选择里面的文本范围和元素。在contentEditable
元素中,这会将插入符号置于所需位置。
这适用于所有现代浏览器,以及不仅仅包含文本节点作为后代的元素。我会允许您将start
和end
的检查添加到适当的范围内。
对于IE8及更低版本,事情有点困难。事情看起来有点像这样:
var setSelectionRange = function(element, start, end) {
var rng = document.body.createTextRange();
rng.moveToElementText(element);
rng.moveStart("character", start);
rng.moveEnd("character", end - element.innerText.length - 1);
rng.select();
};
这里的问题是innerText
不适合这种事情,因为有些空格会崩溃。如果只有一个文本节点,那就很好了,但是对于像contentEditable
元素中的那些更复杂的东西,这些东西都很复杂。
IE8不支持textContent
,因此您必须使用TreeWalker
计算字符数。但IE8也不支持TreeWalker
,所以你必须自己走DOM树...
我仍然需要解决这个问题,但不知怎的,我怀疑自己永远不会。即使我 在IE8中为TreeWalker
代码填充以及更低......
答案 2 :(得分:2)
这是我对蒂姆答案的改进。它删除了关于隐藏字符的警告,但其他警告仍然存在:
代码:
var setSelectionByCharacterOffsets = null;
if (window.getSelection && document.createRange) {
setSelectionByCharacterOffsets = function(containerEl, start, end) {
var charIndex = 0, range = document.createRange();
range.setStart(containerEl, 0);
range.collapse(true);
var nodeStack = [containerEl], node, foundStart = false, stop = false;
while (!stop && (node = nodeStack.pop())) {
if (node.nodeType == 3) {
var hiddenCharacters = findHiddenCharacters(node, node.length)
var nextCharIndex = charIndex + node.length - hiddenCharacters;
if (!foundStart && start >= charIndex && start <= nextCharIndex) {
var nodeIndex = start-charIndex
var hiddenCharactersBeforeStart = findHiddenCharacters(node, nodeIndex)
range.setStart(node, nodeIndex + hiddenCharactersBeforeStart);
foundStart = true;
}
if (foundStart && end >= charIndex && end <= nextCharIndex) {
var nodeIndex = end-charIndex
var hiddenCharactersBeforeEnd = findHiddenCharacters(node, nodeIndex)
range.setEnd(node, nodeIndex + hiddenCharactersBeforeEnd);
stop = true;
}
charIndex = nextCharIndex;
} else {
var i = node.childNodes.length;
while (i--) {
nodeStack.push(node.childNodes[i]);
}
}
}
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}
} else if (document.selection) {
setSelectionByCharacterOffsets = function(containerEl, start, end) {
var textRange = document.body.createTextRange();
textRange.moveToElementText(containerEl);
textRange.collapse(true);
textRange.moveEnd("character", end);
textRange.moveStart("character", start);
textRange.select();
};
}
var x = document.getElementById('a')
x.focus()
setSelectionByCharacterOffsets(x, 1, 13)
function findHiddenCharacters(node, beforeCaretIndex) {
var hiddenCharacters = 0
var lastCharWasWhiteSpace=true
for(var n=0; n-hiddenCharacters<beforeCaretIndex &&n<node.length; n++) {
if([' ','\n','\t','\r'].indexOf(node.textContent[n]) !== -1) {
if(lastCharWasWhiteSpace)
hiddenCharacters++
else
lastCharWasWhiteSpace = true
} else {
lastCharWasWhiteSpace = false
}
}
return hiddenCharacters
}