백스페이스 : 커서 위치에서 삭제
페이지 정보
본문
백스페이스 : 커서 위치에서 삭제
| 소스예제 |
|---|
Tag And Script
<pre class="prettyprint lang-js linenums notranslate pre-word-wrap pretty-total-17" data-li_to="17" data-wrap="pre-word-wrap" data-lang="js"><script>
function backspaceAtCursor(obj) {
var $input = $(obj);
var start = $input[0].selectionStart;
var end = $input[0].selectionEnd;
// 텍스트가 선택되어 있다면 선택된 텍스트 삭제, 아니면 하나 앞 문자 삭제
if (start === end && start > 0) {
$input.val( $input.val().slice(0, start - 1) + $input.val().slice(end) );
$input.selectionStart = $input[0].selectionEnd = start - 1;
} else {
$input.val( $input.val().slice(0, start) + $input.val().slice(end) );
$input.selectionStart = $input[0].selectionEnd = start;
}
$input.focus();
}
</script></pre><pre class="prettyprint lang-html linenums notranslate pre-word-wrap pretty-total-2" data-li_to="2" data-wrap="pre-word-wrap" data-lang="html"><input type="text" id="ex_text" class="frm_input" value="커서 위치에서 삭제" style="width:80%;" />
<button type="button" class="btn" onclick="backspaceAtCursor('#ex_text');">backspace</button></pre>
- 이전글 textarea 현재커서 위치에 내용 삽입 26.03.06
- 다음글 Textarea Tab 들여쓰기 구현 26.03.05
