【发布时间】:2015-07-30 21:31:58
【问题描述】:
我只是想知道如何让这段代码在 IE11 上运行以返回文本字段中的当前光标位置:
getCursorPosition: function() {
var s, e, r;
if(this.inputTextElement.createTextRange){
r = document.selection.createRange().duplicate();
r.moveEnd('character', this.inputTextElement.value.length);
if(r.text === ''){
s = this.inputTextElement.value.length;
} else {
s = this.inputTextElement.value.lastIndexOf(r.text);
}
r = document.selection.createRange().duplicate();
r.moveStart('character', -this.inputTextElement.value.length);
e = r.text.length;
} else {
s = this.inputTextElement.selectionStart;
e = this.inputTextElement.selectionEnd;
}
return this.CursorPosition(s, e, r, this.inputTextElement.value);
},
【问题讨论】:
-
你有没有弄清楚它的哪一部分不起作用?
-
IE11运行第一个if语句,然后在document.selection.createRange().duplicate()处返回错误; IE11 不支持 document.selection,但我不知道如何使用 window.getSelection();
标签: javascript internet-explorer-11