问题是这样的
有一个客户端的input type = 'text' 的客户端文本输入框,里面的value值得是123456
当前的光标停在3和4之间,我现在想通过一个按钮,把一个新的值'9'插入到光标的所在位置,
请问有什么办法呢??大家教教我吧...谢谢

解决方案 »

  1.   

    <HTML>
    <HEAD>
    <SCRIPT>
    function storeCaret(textEl)
    {
    if(textEl.createTextRange)
    textEl.caretPos = document.selection.createRange().duplicate();
    }
    function insertAtCaret(textEl,text){
    if(textEl.createTextRange && textEl.caretPos){
    var caretPos = textEl.caretPos;
    caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == '' ? text + '' : text;
    }
    else
    textEl.value  = text;
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <FORM NAME="aForm">
    <input NAME="aTextArea"  ONSELECT="storeCaret(this);" ONCLICK="storeCaret(this);" ONKEYUP="storeCaret(this);"><BR>
    <INPUT TYPE="text" NAME="aText" SIZE="80" VALUE="要插入的文字">
    <BR>
    <INPUT TYPE="button" VALUE="在光标处插入" ONCLICK="insertAtCaret(this.form.aTextArea,this.form.aText.value);">
    </FORM>
    </BODY>
    </HTML>
      

  2.   

    <%@ Page Language="C#" EnableViewState="true" Debug="true" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">  protected void Page_Load(object sender, EventArgs e)
      {
        TextBox1.Attributes.Add("onselect", "storeCaret()");
        TextBox1.Attributes.Add("onclick", "storeCaret()");
        TextBox1.Attributes.Add("onkeyup", "storeCaret()");
      }
    </script><html xmlns="http://www.w3.org/1999/xhtml">
    <head>  <script>
    function storeCaret()
    {
    var a = document.getElementById("<%=TextBox1.ClientID%>")
    var b = document.getElementById("<%=TextBox2.ClientID%>")
    if(a.createTextRange)
    a.caretPos = document.selection.createRange().duplicate();
    }function insertAtCaret(){
    var a = document.getElementById("<%=TextBox1.ClientID%>")
    var b = document.getElementById("<%=TextBox2.ClientID%>")
    text = b.value
    if(a.createTextRange && a.caretPos){
    var caretPos = a.caretPos;
    caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == '' ? text + '' : text;
    }
    else
    a.value  = text;
    }
      </script></head>
    <body>
      <form runat="server">
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <asp:TextBox ID="TextBox2" runat="server">要插入的文字</asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" OnClientClick="insertAtCaret();" Text="在光标处插入" />
      </form>
    </body>
    </html>