js控制光标到指定节点位置(适用于富文本编辑器中)
function placeCaretAtEnd(el) { //传入光标要去的jq节点对象
el.focus();
if (typeof window.getSelection != "undefined" && typeof document.createRange != "undefined") {
var range = document.createRange();
range.selectNodeContents(el);
range.collapse(false);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} else if (typeof document.body.createTextRange != "undefined") {
var textRange = document.body.createTextRange();
textRange.moveToElementText(el);
textRange.collapse(false);
textRange.select();
}
}
---------------------
作者:国民王九蛋
来源:CSDN
原文:https://blog.csdn.net/weixin_41288457/article/details/79390660
版权声明:本文为博主原创文章,转载请附上博文链接!