I haven't posted any code for a long time. I am in a good mood today. I moved out to bask in the sun.
VaR getcursorendposition = function (textarea) {textarea. focus (); // first of all, you must select if (textarea. setselectionrange) {// W3C return textarea. selectionend;} else if (document. selection) {// ie var I = 0, OS = document. selection. createRange (), // The current selected area, records the information of the current cursor start and end positions, and can be compared with or to get the current cursor position. OR = Document. Body. createTextRange (); // unavailable or = textarea. createTextRange (); otherwise, compareendpoints cannot be performed with the OS. Or. movetoelementtext (textarea); OS. getbookmark (); for (I = 0; or. compareendpoints ("starttostart", OS) <0 & OS. movestart ("character",-1 )! = 0; I ++) {If (textarea. value. charat (I) = '/N') {// line feed + 1. I ++ ;}} return I ;}}var insertstring = function () {var $ comment =$ ("# comment "); vaR endposition = getcursorendposition ($ comment [0]); var key = This. classname, textvalue = $ comment [0]. value; textvalue = textvalue. substring (0, endposition) + key + textvalue. substring (endposition, textvalue. length); $ ("# comment" + name ). val (textvalue); if ($. browser. MSIE) {// when a change occurs, the position should be-1. Endposition-= textvalue. split ('/N '). length-1; var OR = $ comment [0]. createTextRange (); or. collapse (true); or. movestart ('character ', endposition + 6); or. select ();} else {$ comment [0]. setselectionrange (endposition + 6, endposition + 6 );}});}
This article uses b3log solo for Synchronous publishing from Vanessa. Original address: http://vanessa.b3log.org/textarea-cursor-insert-string