[在Safari中使用.splitText()
分割文本节点(在键入时)时,插入符号将移动到新创建的节点之前-在Chrome和Firefox中,按预期将其位置保持在行的末尾。
我想知道,我是否遇到过错误,什么是干净的解决方法?
function wrapWorld({target}) {
const node = target.childNodes[0];
const pos = node.nodeValue.indexOf('world');
console.log(pos);
if (pos > 0) {
//watch the caret in Safari jump before "world" when split :(
const newNode = node.splitText(pos);
}
}
#editor {
width: 300px;
height: 130px;
border: 1px solid #ccc;
}
<p>Type "hello world" in Safari, watch the caret jump before "world" when it gets split</p>
<div id="editor"
contenteditable="true"
oninput="wrapWorld(event)">
</div>
我无法确定这是否是一个错误,我没有在规格中进行广泛的搜索来知道应该在这里发生什么(内容可编辑的内容通常设置不当...),但是两者行为对我来说听起来很合逻辑,就像我希望如果刚刚删除内容后光标会返回。