我需要使用键盘的向右/向左选项逐个输入文本的字符来移动光标。我在IE 11浏览器中使用了angular 5。下面是我的代码。任何人都可以让我知道这是否是正确的方法吗?
var key = event.which || event.keyCode;
if(key === 37 || key === 39){
event.stopPropagation();
let inputDocument = document.getElementById('numericinput');
let range = document.createRange();
range.collapse(true);
let iCaretPos = 0;
if(key === 39 && this.iCaretPos < inputDocument.innerHTML.length){
console.log("inside if");
this.iCaretPos = this.iCaretPos +1;
range.setEnd(input.firstChild, 0);
range.setStart(input.firstChild, this.iCaretPos);
}else if(key === 37 && iCaretPos > 0){
console.log("inside else");
this.iCaretPos = this.iCaretPos -1;
range.setEnd(input.firstChild, 0);
range.setStart(input.firstChild, this.iCaretPos);
}
window.getSelection().removeAllRanges;
window.getSelection().addRange(range);
}
}