textarea 커서 위치 텍스트 삽입
textarea.focus()
FireFox:모든 텍스트 끝
IE: 텍스트 시작
Opera:텍스트 시작
Chrome:텍스트 시작
Safari:텍스트 시작
IE는 document을 지원합니다.selection,Firefox,Chrome,Safari,Opera는 모두 selectionStart와 selectionEnd 속성을 가지고 있다.
다음과 같은 브라우저 특성을 판단하고 구현합니다.
function insertText(obj,str) {
if (document.selection) {
var sel = document.selection.createRange();
sel.text = str;
} else if (typeof obj.selectionStart === 'number' && typeof obj.selectionEnd === 'number') {
var startPos = obj.selectionStart,
endPos = obj.selectionEnd,
cursorPos = startPos,
tmpStr = obj.value;
obj.value = tmpStr.substring(0, startPos) + str + tmpStr.substring(endPos, tmpStr.length);
cursorPos += str.length;
obj.selectionStart = obj.selectionEnd = cursorPos;
} else {
obj.value += str;
}
}
function moveEnd(obj){
obj.focus();
var len = obj.value.length;
if (document.selection) {
var sel = obj.createTextRange();
sel.moveStart('character',len);
sel.collapse();
sel.select();
} else if (typeof obj.selectionStart == 'number' && typeof obj.selectionEnd == 'number') {
obj.selectionStart = obj.selectionEnd = len;
}
}
참조:
http://foxling.org/js-ajax-dom/cross-browser-cursor-position-in-textarea/
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
가난한 사람의 바이오닉 판독나는 이 매우 멋지다는 것을 알았습니다. 그러나 그들의 Chrome 확장 프로그램은 사용하기에 그리 즐겁지 않습니다. 그래서 빠른 확장 프로그램을 만듭니다. 그러나 알고리즘은 원래 알고리즘만큼 복잡하지 않습니다. 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.