http://www.css88.com/
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> 选择、点击文本:<textarea id="description">和设计师一样细腻,和程序员一样严谨;游走在设计师和工程师之间,游刃与用户体验和技术实现之间,斡旋在用户利益和商业利益之间; 网名:愚人码头 QQ:148246293; msn:feiwen8772@hotmail.com(目前基本不上); email:feiwen8772@hotmail.com;</textarea><br /> start: <input id="start" size="3" type="text"> end: <input id="end" size="3" type="text"><br /> 所选字符:<textarea id="text"></textarea> <script src="jquery-1.5.2.min.js"></script> <script> (function ($) { $.fn.selection = function () { var s, e, range, stored_range; // ie if (this[0].selectionStart == undefined) { var selection = document.selection; if (this[0].tagName.toLowerCase() != "textarea") { var val = this.val(); // duplicate()复制 range = selection.createRange().duplicate(); range.moveEnd("character", val.length); // lastIndexOf最后出现的位置 s = (range.text == "" ? val.length : val.lastIndexOf(range.text)); range = selection.createRange().duplicate(); range.moveStart("character", -val.length); e = range.text.length; } else { range = selection.createRange(), stored_range = range.duplicate(); stored_range.moveToElementText(this[0]); stored_range.setEndPoint('EndToEnd', range); s = stored_range.text.length - range.text.length; e = s + range.text.length; } // 非ie } else { s = this[0].selectionStart, e = this[0].selectionEnd; } var te = this[0].value.substring(s, e); return {start: s, end: e, text: te}; }; })(jQuery); $(function () { $("#description").select(function () { $("#start").val($("#description").selection().start); $("#end").val($("#description").selection().end); $("#text").val($("#description").selection().text); }); $("#description").click(function () { $("#start").val($("#description").selection().start); }); $("#description").focus(function () { $("#start").val($("#description").selection().start); }); }); </script> </body> </html>