目前我是使用如下方式来获取光标在txtobj中的位置var cursurPosition = txtobj.getCurPos();可是像下图中光标位置,我在IE678下看到cursurPosition等于1,IE9下等于3
我向当前位置插入字符串进行测试,发现textarea中保存的情况如下:IE 678下内容为:"1\n\n测试2"
IE 9下内容为:  "1测试\n\n2"请教一下,为了兼容所有浏览器,这个该如何处理呢?   IE678里面,果断的用getCurPos()的时候无视了\n这个换行符求指导。JSIE6JavaScriptjs兼容

解决方案 »

  1.   

    上面说错一个地方
    IE 678下内容为:"1\n\n测试2"
    IE 9下内容为:  "1测试\n\n2"
    应该是
    IE 9下内容为:"1\n\n测试2"
    IE 678下内容为:  "1测试\n\n2"
      

  2.   

    这是我以前一个项目的仿键盘输入的一些源码,这几个核心方法也是国外找到的.通过ie6-ie9,其它只测试过Chrome, 外边只要保存好 Range 就可以了 /**
    *
    *@method getInputSelection
    *@param el{HTML-EL}
    *@param range{textRange}
    *@return {Hash} (.start=>uint .end=>uint)
    */
    'getInputSelection' : function (el, range) { var start = 0,
    end = 0,
    normalizedValue,
    textInputRange,
    len,
    endRange; if (typeof el.selectionStart === "number" && typeof el.selectionEnd === "number") {
    start = el.selectionStart;
    end = el.selectionEnd;
    } else {
    !range && (range = document.selection.createRange());
    if (range && range.parentElement() === el) {
    len = el.value.length;
    normalizedValue = el.value.replace(/\r\n/g, "\n"); // Create a working TextRange that lives only in the input
    textInputRange = el.createTextRange();
    textInputRange.moveToBook(range.getBook()); // Check if the start and end of the selection are at the very end
    // of the input, since moveStart/moveEnd doesn't return what we want
    // in those cases
    endRange = el.createTextRange();
    endRange.collapse(false); if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
    start = end = len;
    } else {
    start = -textInputRange.moveStart("character", -len);
    start += normalizedValue.slice(0, start).split("\n").length - 1; if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) {
    end = len;
    } else {
    end = -textInputRange.moveEnd("character", -len);
    end += normalizedValue.slice(0, end).split("\n").length - 1;
    }
    }
    }
    } return {
    start : start,
    end : end
    };
    }, /**
    *
    *@method setInputSelection
    *@param el{HTML-EL}
    *@param start{Uint}
    *@param end{Uint}
    *@return {Hash}
    */
    'setInputSelection' : function (el, startOffset, endOffset) {
    if (typeof el.selectionStart === "number" && typeof el.selectionEnd === "number") {
    el.selectionStart = startOffset;
    el.selectionEnd = endOffset;
    } else {
    var range = el.createTextRange();
    var startCharMove = this.offsetTo(el, startOffset);
    range.collapse(true);
    if (startOffset === endOffset) {
    range.move("character", startCharMove);
    } else {
    range.moveEnd("character", this.offsetTo(el, endOffset));
    range.moveStart("character", startCharMove);
    }
    range.select();
    }
    }, /**
    * 配合 setInputSelection,使用
    *@method offsetTo
    *@param el{HTML-EL}
    *@param offset{Uint}
    *@private
    *@return {Uint}
    */
    'offsetTo' : function (el, offset) {
    return offset - (el.value.slice(0, offset).split("\r\n").length - 1);
    }
      

  3.   

    我昨天才发了这个的
    http://bbs.csdn.net/topics/390477099