var cursor = 0; // 光标位置
document.onselectionchange = function () {
var range = document.selection.createRange();
var srcele = range.parentElement();//获取到当前元素
var copy = document.body.createTextRange();
copy.moveToElementText(srcele);
lyTXT1.focus();
var r = document.selection.createRange();
//因为这里死从当前光标开始移动的(好像文本框的是从0算起.)所以我们需要拿到当前光标位置,然后就可以计算出要移动多少位了,这样就可以把光标移动到想要的位置了
r.moveStart('character', lyTXT1.innerText.length - cursor);
r.collapse(true);
r.select();
}
通过上面的我们就可以将DIV中的光标移动到最后面了
一个完整的实例
代码如下
复制代码
function insertHtmlAtCaret(html) {
var sel, range;
if (window.getSelection) {
// IE9 and non-IE
sel = window.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
range = sel.getRangeAt(0);
range.deleteContents();
// Range.createContextualFragment() would be useful here but is
// non-standard and not supported in all browsers (IE9, for one)
var el = document.createElement(“div”);
el.innerHTML = html;
var frag = document.createDocumentFragment(), node, lastNode;
while ( (node = el.firstChild) ) {
lastNode = frag.appendChild(node);
}
range.insertNode(frag);
// Preserve the selection
if (lastNode) {
range = range.cloneRange();
range.setStartAfter(lastNode);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
}
}
} else if (document.selection && document.selection.type != “Control”) {
// IE < 9
document.selection.createRange().pasteHTML(html);
}
}