SOURCE

console 命令行工具 X clear

                    
>
console
function addImg() {
    let input = document.getElementById('inputRef')
    input.innerHTML += '<img src="https://fe.ecool.fun/static/leibusi.ac7fa5ed.jpeg" />'
    input.focus()
    keepLastIndex(input)
}
function keepLastIndex(obj) {
    if (window.getSelection) {//ie11 10 9 ff safari
        obj.focus(); //解决ff不获取焦点无法定位问题
        var range = window.getSelection();//创建range
        range.selectAllChildren(obj);//range 选择obj下所有子内容
        range.collapseToEnd();//光标移至最后
    }
    else if (document.selection) {//ie10 9 8 7 6 5
        var range = document.selection.createRange();//创建选择对象
        //var range = document.body.createTextRange();
        range.moveToElementText(obj);//range定位到obj
        range.collapse(false);//光标移至最后
        range.select();
    }
}
<div class="inputDiv" contenteditable="true" id="inputRef"></div>
<button onclick="addImg()">添加表情</button>
.inputDiv{
    border: 1px solid #ccc;
    min-height: 30px;
    height: auto;
    background: #fff;
    padding: 8px;
    line-height: 30px;
    vertical-align: middle;
}
.inputDiv img{
    width: 20px;
    height: 20px;
    margin-bottom: -4px;
}