申明:摘自网上,记录下
//在鼠标光标处插入内容
//var v = this.value;
//var field = document.getElementById('editArea');
function contextInsert(field,v){
if (document.selection) {//IE
field.focus();
var sel = document.selection.createRange();
sel.text = v;
sel.select();
}else if (field.selectionStart || field.selectionStart == '0') {//MOZILLA/GOOGLE
var startPos = field.selectionStart;
var endPos = field.selectionEnd;
var restoreTop = field.scrollTop;
field.value = field.value.substring(0, startPos) + v + field.value.substring(endPos, field.value.length);
if (restoreTop > 0) {
field.scrollTop = restoreTop;
}
field.focus();
field.selectionStart = startPos + v.length;
field.selectionEnd = startPos + v.length;
} else {//OTHER
field.value += v;
field.focus();
}
}