码迷,mamicode.com
首页 > 其他好文 > 详细

input光标位置

时间:2016-11-02 17:53:45      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:bss   bow   bst   dmi   document   cst   cgo   ace   ams   

兼容谷歌火狐-input光标位置

input框在没有添加任何效果的情况下,输入文字后光标始终在最后的位置,谷歌||火狐效果一样

但是在给input加入点击事件后   谷歌:input框插入文字后,光标会自动到最后位置

                火狐:input框插入文字后,光标在插入文字的后面

兼容:光标在文字的最后面

  function moveEnd(obj){
  obj.focus();
  var len = obj.value.length;
  if (document.selection) {
  var sel = obj.createTextRange();
  sel.moveStart(‘character‘,len);
  sel.collapse();
  sel.select();
  } else if (typeof obj.selectionStart == ‘number‘ && typeof obj.selectionEnd == ‘number‘) {
  obj.selectionStart = obj.selectionEnd = len;
    }
  }
技术分享

 兼容:光标在中间插入文字的后面      

   function moveEnd(obj){
    obj.focus();
    var len = obj.value.length;
    if (document.selection) {
    var sel = obj.createTextRange();
    sel.moveStart(‘character‘,len);
    sel.collapse();
    sel.select();
    } else if (typeof obj.selectionStart == ‘number‘ && typeof obj.selectionEnd == ‘number‘) {
    
  setCursorPosition(obj,obj.selectionStart)

      }

    }技术分享

  设置光标位置:

  setSelectionRange(obj.selectionStart,obj.selectionStart);

  selectionStart
      输入性元素selection起点的位置,可读写。
  selectionEnd
      输入性元素selection结束点的位置,可读写。
  setSelectionRange(start, end)
      设置输入性元素selectionStart和selectionEnd值

 

input光标位置

标签:bss   bow   bst   dmi   document   cst   cgo   ace   ams   

原文地址:http://www.cnblogs.com/Kyaya/p/6023369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!