源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

js获取光标位置和设置文本框光标位置示例代码

  • 时间:2022-09-17 03:57 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:js获取光标位置和设置文本框光标位置示例代码
[u]复制代码[/u] 代码如下:
<script type="text/javascript">         function getTxt1CursorPosition(){             var oTxt1 = document.getElementById("txt1");             var cursurPosition=-1;             if(oTxt1.selectionStart){//非IE浏览器                 cursurPosition= oTxt1.selectionStart;             }else{//IE                 var range = document.selection.createRange();                 range.moveStart("character",-oTxt1.value.length);                 cursurPosition=range.text.length;             }             alert(cursurPosition);         }         function setTxt1CursorPosition(i){             var oTxt1 = document.getElementById("txt2");             var cursurPosition=-1;             if(oTxt1.selectionStart){//非IE浏览器                 oTxt1.selectionStart=i;             }else{//IE                 var range = oTxt1.createTextRange();                 range.move("character",i);                 range.select();             }         }         function getTa1CursorPosition(){             var evt =window.event?window.event:getTa1CursorPosition.caller.arguments[0];             var oTa1 = document.getElementById("ta1");             var cursurPosition=-1;             if(oTa1.selectionStart){//非IE浏览器                 cursurPosition= oTa1.selectionStart;             }else{//IE                 var range = oTa1.createTextRange();                 range.moveToPoint(evt.x,evt.y);                 range.moveStart("character",-oTa1.value.length);                 cursurPosition=range.text.length;             }             alert(cursurPosition);         }         function setTa1CursorPosition(i){             var oTa2 = document.getElementById("ta2");             if(oTa2.selectionStart){//非IE浏览器                 oTa2.selectionStart=i;                 oTa2.selectionEnd=i;             }else{//IE                 var range = oTa2.createTextRange();                 range.move("character",i);                 range.select();             }         }     </script>
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部