IE's document.selection.createRange doesn't include leading or trailing blank lines

前端 未结 4 1482
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 09:18

I\'m trying to extract the exact selection and cursor location from a textarea. As usual, what\'s easy in most browsers is not in IE.

I\'m using this:

         


        
4条回答
  •  無奈伤痛
    2020-11-30 10:10

    The move by negative bazillion seems to work perfectly.

    Here's what I ended up with:

    var sel=document.selection.createRange();
    var temp=sel.duplicate();
    temp.moveToElementText(textarea);
    var basepos=-temp.moveStart('character', -10000000);
    
    this.m_selectionStart = -sel.moveStart('character', -10000000)-basepos;
    this.m_selectionEnd = -sel.moveEnd('character', -10000000)-basepos;
    this.m_text=textarea.value.replace(/\r\n/gm,"\n");
    

    Thanks bobince - how can I vote up your answer when it's just a comment :(

提交回复
热议问题