This article illustrates the way jQuery inserts text into the location of the cursor in textarea.Share for you for your reference.The concrete realization method is as follows:






$(函数(){())
插入文本 / * * /文本开始
(函数($){)
fn.extend({美元。
insertcontent:功能(自我价值,T){
var $ $ =($){ 0 };
如果(文档选择){伊江
This.focus();
VaR的SEL = document.selection.createrange();
sel.text =自我价值;
This.focus();
Sel.moveStart(字符,L);
VaR Wee = sel.text.length;
如果(arguments.length = = 2){
var = t.value.length美元;
Sel.moveEnd(人物
0 sel.movestart(人物
Sel.select();
}
其他($ t.selectionstart如果}
| | t.selectionstart美元= = '0'){
VaR StartPos = t.selectionstart美元;
VaR EndPos = t.selectionend美元;
VaR scrollTop = $ t.scrolltop;
t.value美元=美元(0,让t.value.substring)
+自我价值
+ $(美元t.value.substring EndPos,t.value。长度);
This.focus();
t.selectionstart美元= Startpos + myvalue.length;
t.selectionend美元= Startpos + myvalue.length;
t.scrolltop美元= scrollTop;
如果(arguments.length = = 2){
让t.setselectionrange美元(T,
t.selectionend美元+ T);
This.focus();
}
{人}
this.value =自我价值;
This.focus();
}
}
})
}(jQuery);
插入文本 / * * /文本--结束
});
$(文档)Ready(函数(){)
$(# ch_button)。Click(function(){)
$(# test_in)。InsertContent();
});
});



插入








希望本文能对大家的jQuery程序设计有所帮助。