텍스트 영역의 마지막 위치를 항상 기억하고 싶지만 항상 실패합니다.텍사 레아에서의 위치 기억하는 법?
나는이 코드를 가지고 : 내가 작성하는 경우,
//This function catch the position
(function ($, undefined) {
$.fn.getCursorPosition = function() {
var el = $(this).get(0);
var pos = 0;
if('selectionStart' in el) {
pos = el.selectionStart;
} else if('selection' in document) {
el.focus();
var Sel = document.selection.createRange();
var SelLength = document.selection.createRange().text.length;
Sel.moveStart('character', -el.value.length);
pos = Sel.text.length - SelLength;
}
return pos;
}
})(jQuery);
//Insert the text by clicking an icon
function insertar(texto) {
var div = $('textarea');
var text = div.val();
var pos = div.getCursorPosition();
text = text.substring(0,pos) + " " + texto + text.substring(pos);
div.val(text);
}
$('#icon').click(function(evt){ insertar(':icon:'); });
음을이 :
"안녕하세요 안녕하세요"
그리고 나는 기능이 추가하려면 :
"안녕하세요 안녕하세요 안녕하세요"
내게 :
"hi hello hello hi."
두 번째로 함수를 실행하면 위치를 잊어 버리고 텍스트의 끝에 텍스트를 추가한다고 생각합니다.
PD : 이미지를 클릭하면 텍스트가 추가됩니다. 나는 포럼을 위해 빠른 답장 fieldset을하려고 노력하고있다.
누구나 어디에서 문제가 발생했는지 알 수 있습니까? http://jsfiddle.net/ko295zpw/6/
그래서 당신은 당신이 앞에 텍스트를 삽입 할 뜻 업데이트? .. like this http://jsfiddle.net/ko295zpw/8/ – Dave
[selectionEnd] (https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement). – Teemu
아니, Dave. 한 번 클릭하거나 다른 위치에서 클릭하고 원하는 위치에 나머지 텍스트는 그대로두고 싶습니다. 모든 포럼에서 찾을 수있는 빠른 답장 편집기와 같습니다. – Jase