sanshi 发表于 2013-5-27 17:55
获取光标位置这样的操作只能在前台实现了,网上搜下了,应该有现成的实现 ... - <script type="text/javascript" defer="defer">
- var nad = '<%=Text_DT_HTML.ClientID%>';
- function onReady() {//添加事件
- $('#' + nad).keydown(function (event) {
- getCursorPosition(nad);
- });
- $('#' + nad).keyup(function (event) {
- getCursorPosition(nad);
- });
- $('#' + nad).mousedown(function (event) {
- getCursorPosition(nad);
- });
- $('#' + nad).mouseup(function (event) {
- getCursorPosition(nad);
- });
- $('#' + nad).focus(function (event) {
- getCursorPosition(nad);
- });
- }
- function getCursorPosition(obj) {//获取光标位置
- var evt = window.event ? window.event : getTa1CursorPosition.caller.arguments[0];
- var oTa1 = document.getElementById(obj);
- try {
- if (oTa1.value == "")
- return 0;
- var cursurPosition = -1;
- if (oTa1.selectionStart) {//非IE浏览器
- cursurPosition = oTa1.selectionStart;
- } else {//IE
- var rngSel = document.selection.createRange();//建立选择域
- var rngTxt = oTa1.createTextRange();//建立文本域
- var flag = rngSel.getBookmark();//用选择域建立书签
- rngTxt.collapse();//瓦解文本域到开始位,以便使标志位移动
- rngTxt.moveToBookmark(flag);//使文本域移动到书签位
- rngTxt.moveStart('character', -oTa1.value.length);//获得文本域左侧文本
- cursurPosition = rngTxt.text.replace(/\r\n/g, '').length;//替换回车换行符
- }
- //return cursurPosition;
- }
- catch (e) {
- cursurPosition = oTa1.value.length;
- }
- document.getElementById("<%=TextBox_gb.ClientID%>").innerText = cursurPosition;
- }
- function setCaretPosition(pos) {//设置光标位置函数
- var ctrl = nad;
- if (ctrl.setSelectionRange) {
- ctrl.focus();
- ctrl.setSelectionRange(pos, pos);
- }
- else if (ctrl.createTextRange) {
- var range = ctrl.createTextRange();
- range.collapse(true);
- range.moveEnd('character', pos);
- range.moveStart('character', pos);
- range.select();
- }
- }
- </script>
复制代码 js搞定....和框架兼容.... |