Open ZhengXingchi opened 4 years ago
the simplest and smallest WYSIWYG text editor for web, with no dependencies 一个短小精悍没有任何依赖的浏览器富文本编辑器 readme中有各个富文本编辑器的对比 jaredreich/pell的github地址
这篇文章很有启发意义javascript – focus()在边缘浏览器中不起作用
(仿多说留言、评论框,带微博表情) 仿多说留言、评论框,带微博表情。 查看演示
/把光标移到末尾
msgTextLastPos(obj) {
// 解决浏览器的兼容问题,做如下条件判断
if (window.getSelection) {
obj.focus();
let range = window.getSelection();
range.selectAllChildren(obj);
range.collapseToEnd();//光标移至最后
}
else if (document.selection) {
let range = document.selection.createRange();
range.moveToElementText(obj);
range.collapse(false);//光标移至最后
range.select();
}
}
参考自富文本中 移动光标到末尾
本来想参考 focus到文本框尾部上面趁你还年轻
的答案,但是没有效果。
const el = document.getElementById('richTextEditor');
const range = document.createRange();
const sel = window.getSelection();
console.log(document.activeElement);
const tailIndex = el.childNodes.length;
const tailNode = el.childNodes[tailIndex - 1];
range.setStart(tailNode, tailNode.length);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
但是对于和
insertImage
这个命令只能指定路径,无法指定宽高,用insertHTML
document.execCommand('insertHtml' , false , '<img src="'+src+'" width="20px" height="20px">');
参考文献
react-contenteditable React组件实现一个内容可编辑的div
仅用1行核心JS代码实现一个轻量级富文本编辑器 | 拓跋的前端客栈