//获取光标
function doGetCaretPosition (oField) { // Initialize var iCaretPos ; // IE Support if (document.selection) { // Set focus on the element oField.focus(); // To get cursor position, get empty selection range var oSel = document.selection.createRange(); // Move selection start to 0 position oSel.moveStart('character', -oField.value.length); // The caret position is selection length iCaretPos = oSel.text.length; } // Firefox support else if (oField.selectionStart || oField.selectionStart == '0') iCaretPos = oField.selectionStart; console.log(iCaretPos) // Return results return iCaretPos; }
//截取字符串
String.prototype.insert = function (index, item) {
var temp = [];
for (var i = 0; i < this.length; i++) {
temp.push(this[i]);
}
temp.splice(index, 0, item);
console.log(temp.join(""))
return temp.join("")
};
//js
string.insert(0,'jia')