-
커서위치에 글자(문자) 삽입하기 Insert script at cursor positionWeb/js, jQuery 2017. 4. 21. 18:59반응형
커서위치에 글자(문자) 삽입하기
Insert script at cursor position
매게변수는 접근아이디, 텍스트
function insertAtCursor(areaId,text)
{
var txtarea = document.getElementById(areaId);
var scrollPos = txtarea.scrollTop;
var strPos = 0;
var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ?
"ff" : (document.selection ? "ie" : false ) );
if (br == "ie") {
txtarea.focus();
var range = document.selection.createRange();
range.moveStart ('character', -txtarea.value.length);
strPos = range.text.length;
}
else if (br == "ff") strPos = txtarea.selectionStart;
var front = (txtarea.value).substring(0,strPos);
var back = (txtarea.value).substring(strPos,txtarea.value.length);
txtarea.value=front+text+back;
strPos = strPos + text.length;
if (br == "ie") {
txtarea.focus();
var range = document.selection.createRange();
range.moveStart ('character', -txtarea.value.length);
range.moveStart ('character', strPos);
range.moveEnd ('character', 0);
range.select();
}
else if (br == "ff") {
txtarea.selectionStart = strPos;
txtarea.selectionEnd = strPos;
txtarea.focus();
}
txtarea.scrollTop = scrollPos;
}
반응형'Web > js, jQuery' 카테고리의 다른 글
아이프레임이(+안에 이미지 포함) 로드되었을 때 이벤트 실행 (0) 2018.01.22 jQuery Animation Plugin = WOW (0) 2018.01.17 동적으로 생긴 테그에 이벤트 주기(추가된 테그에 이벤트 발생) (0) 2017.04.10 input 안에 숫자 콤마 찍기 (input numberformat, input comma) (0) 2016.10.18 [Jquery][HTML][CSS]간단한 탭메뉴(Tab menu Source) 소스 (0) 2016.09.09