JS 获取文本框焦点所在的索引值

  function getTextFocusIndex(){
      var input = document.getElementById("input1");
      var cursurPosition=0;
      if(input.selectionStart){//非IE
          cursurPosition= input.selectionStart;
      }else{//IE
           try{
                var range = document.selection.createRange();
                range.moveStart("character",-input.value.length);
                cursurPosition=range.text.length;
           }catch(e){
               cursurPosition = 0;
            }
        }
        console.log("当前位置", cursurPosition)
  }

你可能感兴趣的:(JS 获取文本框焦点所在的索引值)