Posted in Javascript onNovember 24, 2012
<script language="javascript" type="text/javascript"> function Button4_onclick(obj) { obj.focus(); var rng=document.selection.createRange(); rng.setEndPoint("StartToStart",obj.createTextRange()); var eng=document.selection.createRange(); eng.setEndPoint("EndToEnd",obj.createTextRange()); var kone = document.getElementByIdx("Text1"); kone.value=rng.text + "插入字符" + eng.text; } </script> <input id="Text1" type="text" value="1235467890"/> <input id="Button4" type="button" value="button" onclick="return Button4_onclick(Text1)" />
<%@ Page Language="C#" AutoEventWireup="true" AspCompat="true" CodeBehind="WebForm1.aspx.cs" Inherits="SJLERP.WebForm1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <script type="text/javascript"> function setCaret(textObj) { if (textObj.createTextRange) { textObj.caretPos = document.selection.createRange().duplicate(); } } function insertAtCaret(textObj, textFeildValue) { if (document.all) { if (textObj.createTextRange && textObj.caretPos) { var caretPos = textObj.caretPos; caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? textFeildValue + ' ' : textFeildValue; } else { textObj.value = textFeildValue; } } else { if (textObj.setSelectionRange) { var rangeStart = textObj.selectionStart; var rangeEnd = textObj.selectionEnd; var tempStr1 = textObj.value.substring(0, rangeStart); var tempStr2 = textObj.value.substring(rangeEnd); textObj.value = tempStr1 + textFeildValue + tempStr2; } else { alert("This version of Mozilla based browser does not support setSelectionRange"); } } } </script> <form id="form1" action="" onsubmit="" method="post" enctype="text/plain"> <p> <textarea name="tarea" rows="" cols="" style="width:300px;height:120px;" onselect="setCaret(this);" onclick="setCaret(this);" onkeyup="setCaret(this);" >例子例子例 Forget Never 子例子例子 asp.net </textarea> <br/><br/> <input type="text" name="textfield" style="width:220px;" value="插入FireFox"/> <br/> <input type="button" value="插入" onclick="insertAtCaret(this.form.tarea,this.form.textfield.value);"/> </p> </form> </html>
js实现在文本框光标处添加字符的方法介绍
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@