<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <ptml xmlns = "http://www.w3.org/1999/xhtml"> <pead> <title> JavaScript get/Set cursor position, compatible with Input & TextArea. </Title> <meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/> <style> body {margin: 32px; font-family: Verdana, sans-serif; font-size: 13px ;}. title {font-size: 18px; font-weight: bolder; margin: 40px 0 ;}. input {width: 50%; font-family: Verdana, sans-serif; font-size: 13px; text-indent: 2px ;} </style> </pead> <body> JavaScript obtains/sets the cursor position. compatible with Input & TextArea: single-line text box <p> <input clas S = "input" id = "textbox" name = "textbox" value = "Hi, www.jb51.net !!! "/> </P> <input type =" button "onclick =" alert (getPositionForInput (document. getElementById ('textbox ') "value =" Get Position "> input Position: <input type = "text" id = "no1" size = "1"/> <input type = "button" onclick = "process ('no1', 'textbox '); "value =" Set Position "> multi-line text box <pre class =" code "id =" zhangdanNum "name =" zhangdanNum "style =" height: 66px; width: 246px; overflow: hidden "> Hi, CssRain !!! </Textarea> <input type = "button" onclick = "alert (getPositionForTextArea (document. getElementById ('hangdannum') "value =" Get Position "> input Position: <input type = "text" id = "no2" size = "1"/> <input type = "button" onclick = "process ('no2 ', 'zhangdannum '); "value =" Set Position "> </body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]