WEB页面有个TEXTBOX,旁边一个按钮如何点击按钮在TEXTBOX内鼠标的指定位置添加内容
比如TEXTBOX内容为:111222333444这时候如果我鼠标如果定位在111后面,点击旁边的按钮TEXTBOX内容显示为:111测试222333444

解决方案 »

  1.   

    <html>
    <head>
    <script type="text/javascript"> 
    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;

    function deleteAtCaret (textEl) {
    if (textEl.createTextRange && textEl.caretPos) {
    var caretPos = textEl.caretPos;
    document.selection.clear();
    }
    //alert(aForm.aTextArea. 

    </script> 
    </head>
    <body>
    <form name=aForm>
    <input name=aTextArea size=120 wrap=soft onselect="storeCaret(this);" onclick="storeCaret(this);" onkeyup="storeCaret(this);" value=这是例子,你可以在这里添加文字、插入文字。> 
    <br> 
    <input type=text name=aText size=80 value=我要在光标处插入这些文字><br>
    <input type=button value=我要在光标处插入上面文本框里输入的文字! onclick="insertAtCaret(this.form.aTextArea, this.form.aText.value);">
    <input type=button value=删除选中文字 onclick=deleteAtCaret(this.form.aTextArea); id=button1 name=button1>
    </form> 
    </body>
    </html>