js 텍스트 상자 커서에 문자를 추가하는 방법 소개

2722 단어
 
  
<br>function Button4_onclick(obj) { <br>obj.focus(); <br>var rng=document.selection.createRange(); <br>rng.setEndPoint("StartToStart",obj.createTextRange()); <br>var eng=document.selection.createRange(); <br>eng.setEndPoint("EndToEnd",obj.createTextRange()); <br>var kone = document.getElementByIdx("Text1"); <br>kone.value=rng.text + " " + eng.text; <br>} <br>

value="button" onclick="return Button4_onclick(Text1)" />
 
  
Inherits="SJLERP.WebForm1" %>


<br>function setCaret(textObj) { <br>if (textObj.createTextRange) { <br>textObj.caretPos = document.selection.createRange().duplicate(); <br>} <br>} <br>function insertAtCaret(textObj, textFeildValue) { <br>if (document.all) { <br>if (textObj.createTextRange && textObj.caretPos) { <br>var caretPos = textObj.caretPos; <br>caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? textFeildValue + ' ' : textFeildValue; <br>} else { <br>textObj.value = textFeildValue; <br>} <br>} else { <br>if (textObj.setSelectionRange) { <br>var rangeStart = textObj.selectionStart; <br>var rangeEnd = textObj.selectionEnd; <br>var tempStr1 = textObj.value.substring(0, rangeStart); <br>var tempStr2 = textObj.value.substring(rangeEnd); <br>textObj.value = tempStr1 + textFeildValue + tempStr2; <br>} else { <br>alert("This version of Mozilla based browser does not support setSelectionRange"); <br>} <br>} <br>} <br>









onclick="insertAtCaret(this.form.tarea,this.form.textfield.value);"/>




좋은 웹페이지 즐겨찾기