3
我有邏輯在contenteditable div插入標籤。我想在插入後將光標設置到以下元素的開頭。我不得不這樣做的代碼是:如何將光標移動到Chrome/Safari中contenteditable div中的下一個元素?
function insertNodeAtCaret(node) {
if(typeof window.getSelection !== 'undefined')
{
var sel = window.getSelection();
if(sel.rangeCount)
{
var rng = sel.getRangeAt(0);
rng.collapse(false);
rng.insertNode(node);
// The following doesn't work in Chrome or Safari
node = node.nextSibling;
rng.setEndAfter(node);
rng.setStartAfter(node);
rng.collapse(true);
sel.removeAllRanges();
sel.addRange(rng);
}
}
else if (typeof document.selection !== 'undefined' && document.selection.type !== 'Control')
{
document.selection.createRange().pasteHTML(node.outerHTML);
}
}
然而,雖然這工作完全在IE,Opera和Firefox不會在Chrome和Safari工作。在Chrome/Safari中,光標位於跨度內的文本末尾,而不是跨度後面。即
<span>text CURSOR HERE</span>
,而不是我想要的東西,那就是:
<span>text</span>CURSOR HERE
感謝。
爲遲到的答覆道歉,我不得不在其他一些項目上工作。只是想對蒂姆表達您的意見表示非常感謝,非常感謝。令人沮喪的是,其中一些問題多年來一直沒有解決。 – fhevol