Get contentEditable caret index position

后端 未结 10 701
暗喜
暗喜 2020-11-22 05:48

I\'m finding tons of good, crossbrowser anwers on how to SET the cursor or caret index position in a contentEditable element, but none on how to GET or find its

10条回答
  •  [愿得一人]
    2020-11-22 06:18

    $("#editable").on('keydown keyup mousedown mouseup',function(e){
    		   
           if($(window.getSelection().anchorNode).is($(this))){
        	  $('#position').html('0')
           }else{
             $('#position').html(window.getSelection().anchorOffset);
           }
     });
    body{
      padding:40px;
    }
    #editable{
      height:50px;
      width:400px;
      border:1px solid #000;
    }
    #editable p{
      margin:0;
      padding:0;
    }
    
    
    move the cursor to see position
    position :

提交回复
热议问题