나는 this post에 설명 된 내용을 구현하려고 시도한 JSFiddle입니다. 그러나 놀랍게도 내 코드는 작동하지 않으며 단지 이유를 이해할 수 없습니다.초점을 맞추고 contentable div의 끝에 커서를 놓습니다.
경우에 따라 동일한 코드를 여기 stackoverflow에 배치합니다.
$(document).ready(function(){
focusAndPlaceCaretAtEnd($('#test'));
});
function focusAndPlaceCaretAtEnd(el) {
el.focus();
if (typeof window.getSelection != "undefined"
&& typeof document.createRange != "undefined") {
var range = document.createRange();
range.selectNodeContents(el);
range.collapse(false);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} else if (typeof document.body.createTextRange != "undefined") {
var textRange = document.body.createTextRange();
textRange.moveToElementText(el);
textRange.collapse(false);
textRange.select();
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="test" contenteditable="true" placeholder="">
Focus and place the cursor to the end
</div>
코드 스 니펫에 오류가 있습니다. 실제 오류가 무엇인지 정확하게 알기 위해 편집하지 않으시겠습니까? (물론 스 니펫의 오류 일 경우 첫 번째 실마리일까요?) –