이 간단한 jquery 플러그인을 jQuery Set Cursor Position in Text Area에서 찾았습니다. 잘 작동합니다. 하지만 한 가지 문제는 파이어 폭스 및 크롬 콘솔에 오류가 발생했습니다.사용자 정의 jquery 플러그인에 대한 브라우저 콘솔 오류
Uncaught TypeError: Object #<HTMLInputElement> has no method 'setCursorPosition'
내가 뭘하는지는 다음과 같이 간단합니다.
<script type="text/javascript">
//to set text cursor
(function($) {
$.fn.setCursorPosition = function(pos) {
if ($(this).get(0).setSelectionRange) {
$(this).get(0).setSelectionRange(pos, pos);
} else if ($(this).get(0).createTextRange) {
var range = $(this).get(0).createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
}
})(jQuery);
....
textboxArray.get(0).setCursorPosition(0);
</script>
위의 오류가 발생하는 이유에 대한 힌트를 알려 주시면 감사하겠습니다.
업데이트 : 블렌더 덕분에 곤란을 겪어 다음과 같은 문제가 해결되었습니다.
textboxArray.setCursorPosition(0);
시도, 당신의 textboxArray 요소의 jQuery 객체가 아닌 것으로 보인다? 그것을 제거하고 어떤 일이 일어나는 지보십시오. – Blender
@Blender 당신은 예민한 눈을가집니다. 잘. 어쩌면, 나는 둔한 것을 가지고 있을지도 모른다. 플러그인은 배열을 필요로합니다. 감사합니다 –
괜찮습니다. 운 좋은 추측 ... – Blender