input光標位置


兼容谷歌火狐-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值

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM