我有这个html:
<div id="myEditor" contenteditable="true">
Hey Jony whats up. Are you up. Also need <a id="89" contenteditable="false">Philly</a> for today's presentation
</div>
如何使用 jQuery 将光标置于“a#89”之前?一直在经历各种答案,但发现它们有点过于复杂。
这是我找到的一个片段,但到目前为止没有任何价值:
function setCursor(contentEditableElement)
{
var range,selection;
if(document.createRange)//Firefox, Chrome, Opera, Safari, IE 9+
{
range = document.createRange();
range.selectNodeContents(contentEditableElement);
range.collapse(false);
selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
}
else if(document.selection)//IE 8 and lower
{
range = document.body.createTextRange();
range.moveToElementText(contentEditableElement);
range.collapse(false);
range.select();
}
}