The code is as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">//Remove all left and right spacesString.prototype.trim =function () { return This. Replace (/(^\s*) | ( \s*$)/g, ""); } functiongetPosition (Element) {varOsobject = ""; if(Navigator.userAgent.indexOf ("MSIE") > 0) {Osobject= "MSIE"; } if(Navigator.userAgent.indexOf ("Firefox") > 0) {Osobject= "Firefox"; } if(Navigator.userAgent.indexOf ("Safari") > 0) {Osobject= "Safari"; } if(Navigator.userAgent.indexOf ("Camino") > 0) {Osobject= "Camino"; } if(Navigator.userAgent.indexOf ("Gecko") > 0) {Osobject= "Gecko"; } if(Navigator.userAgent.indexOf ("Chrome") > 0) {Osobject= "Chrome"; } varresult = 0; if(!document.selection) {//non-IE browser varThistagname =NULL; if(element) [0].nodename! = "TEXTAREA" && $ (Element) [0].nodename! = ' INPUT ') { if(element) [0].nodename = = "DIV" && $ (Element). attr ("contenteditable") = = "true") {Thistagname=window.getselection (). AnchorNode.parentElement.tagName; } Else{thistagname==NULL; } } Else { if(element) [0].nodename = = "INPUT" && $ (Element). attr ("type") = = "Text") {Thistagname=window.getselection (). Anchornode.tagname; } Else{Console.log (window.getselection ()); Thistagname=window.getselection (). Anchornode.tagname; } } if(Thistagname = = "TEXTAREA" | | thistagname = = "INPUT" | | (thistagname== "BODY" &&osobject = = "Chrome") {result=Element.selectionstart; } Else if(Thistagname! =NULL) { if(Thistagname = =element.tagname) {if(Window.getselection (). Anchornode.textcontent = =$ (Element). Text ()) {Result=window.getselection (). Anchoroffset; } Else { varCurrentindex =window.getselection (). Anchoroffset; vartxt = ""; varTxtoo =window.getselection (). anchornode.previoussibling; while(Txtoo! =NULL) {txt+=txtoo.textcontent; Txtoo=txtoo.previoussibling; } result= Txt.length +Currentindex; } } Else { varCurrentindex =window.getselection (). Anchoroffset; vartxt = ""; varTxtoo =window.getselection (). anchorNode.parentElement.previousSibling; while(Txtoo! =NULL) {txt+=txtoo.textcontent; Txtoo=txtoo.previoussibling; } result= Txt.length +Currentindex; } } Else { return0; } } Else{//IE varrng; if(Element). attr ("tagName") = = "TEXTAREA" | | (Element). attr ("tagName") = = "INPUT" && $ (Element). attr ("type") = = "text") | | (Element). attr ("tagName") = = "DIV" && $ (Element). attr ("contenteditable") = = "true") ) {element.focus (); RNG=Document.selection.createRange (); Rng.movestart (' Character ',-element.innerText.length); varText =Rng.text; for(vari = 0; i < element.innerText.length; i++) { if(element.innerText.substring (0, i + 1) = = Text.substring (Text.length-i-1, Text.length)) {Result= i + 1; } } } Else { return0; } } returnresult; } functionGetValue (Element) {varpos =getPosition (Element); document.getElementById ("Pnum"). Value =POS; } </script> <style type= "Text/css" >#Div1, #Div2 {width:500px; height:100px; Border:solid 1px black; } </style>JS gets the position of the edit box cursor