前提・実現したいこと
- ボタンを押すと文字をテキストボックスに追加する
- テキストボックスにフォーカスを当てる
- キャレットの表示を維持する
- キャレットを追従して文字を表示したい
発生している問題
- IE11にテキストボックスの右端を超える文字を追加すると表示状態がキャレットの位置に追従してくれない
該当のソースコード
js
1function addChar(keyChar) { 2 const textArea = document.getElementById('terabox'); 3 const caretPos = textArea.selectionStart; 4 const addedMessage = this.insertChar(textArea.value, caretPos, keyChar); 5 textArea.value = addedMessage; 6 textArea.setSelectionRange(caretPos+1, caretPos+1); 7 textArea.focus(); 8} 9 10function insertChar(messageValue, pos, char){ 11 return messageValue.slice(0, pos) + char + messageValue.slice(pos); 12}
html
1<body> 2 <input id="terabox" type="text" /><br><br> 3 <button onclick="addChar('T')">T</button> 4 <button onclick="addChar('E')">E</button> 5 <button onclick="addChar('R')">R</button> 6 <button onclick="addChar('A')">A</button> 7</body>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/03/23 02:40