我正在使用Jquery表情符号在DIV中插入表情符号(不是在textarea或textbox中),但似乎有一些问题,当我点击其他地方时,光标位置总是会改变,实际上它应该留在当我一直点击其他地方时Div的结束,那么如何解决呢? 这是Html:
<div id="editor" contenteditable="true" aria-placeholder="add something...">
</div>
<div class="m-t-70">
<button class="btn btn-primary m-t-70" id="btnLoad2" >
add or not whatever
</button>
</div>
和js的一部分:
if ("DIV" === a.nodeName) {
if (a.focus(),
b.getSelection) {
if (f = b.getSelection(),
f.getRangeAt && f.rangeCount) {
g = f.getRangeAt(0),
g.deleteContents();
var h = c.createElement("div");
h.innerHTML = d;
for (var i, j, k = c.createDocumentFragment();
i = h.firstChild;)j = k.appendChild(i);
var l = k.firstChild;
g.insertNode(k),
j && (g = g.cloneRange(),
g.setStartAfter(j),
e ? g.setStartBefore(l) : g.collapse(!0),
f.removeAllRanges(),
f.addRange(g))
}
}
else if ((f = c.selection) && "Control" !== f.type) {
var m = f.createRange();
m.collapse(!0),
f.createRange().pasteHTML(html),
e && (g = f.createRange(),
g.setEndPoint("StartToStart", m),
g.select())
}
}
else if (c.selection) a.focus(),
f = c.selection.createRange(),
f.text = d,
f.select();
else if (a.selectionStart || 0 === a.selectionStart) {
var n = a.selectionStart,
o = a.selectionEnd,
p = a.scrollTop;
a.value = a.value.substring(0, n) + d + a.value.substring(o, a.value.length),
p > 0 && (a.scrollTop = p),
a.focus(),
a.selectionStart = n + d.length,
a.selectionEnd = n + d.length
}
else a.value += d,
a.focus()
},