当前位置:编程学习 > C#/ASP.NET >>

分享一个方法:在TextBox中光标位置处插入内容(WEB)

找了好多的资料 现尝试如下,来解决文本框(WEB)在光标位置插入字符等内容,代码简介 共享一下,以免失传:
 <script type="text/javascript">

function AddText()
{
var str = document.getElementById("<%=TextBox1.ClientID %>").value ;
document.getElementById("<%=TextBox2.ClientID %>").focus();
document.execCommand("paste",0,str)
}

</script>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server" Height="121px" Width="258px" 
            TextMode="MultiLine"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="AddText()" />

--------------------编程问答-------------------- 那就收藏了吧 --------------------编程问答-------------------- LZ好人,已经收藏了 --------------------编程问答-------------------- mark --------------------编程问答-------------------- up!!!! --------------------编程问答-------------------- 收藏! --------------------编程问答-------------------- 收藏 --------------------编程问答-------------------- mark --------------------编程问答-------------------- mark --------------------编程问答-------------------- mark --------------------编程问答-------------------- up    收藏 --------------------编程问答-------------------- tag --------------------编程问答-------------------- 失传 --------------------编程问答-------------------- OK收藏了啊 --------------------编程问答-------------------- 收藏 --------------------编程问答-------------------- --------------------编程问答--------------------

//textObj插入的文本对象,textFeildValue值
var insertAtText = function(textObj, textFeildValue) {
            if ($.browser.msie) {//$.browser.msie判断是否是IE
                if (document.selection) {
                    textObj.focus();
                    textObj = document.selection.createRange();
                    textObj.text = textFeildValue;
                }
            } else {
                if (document.all && textObj.createTextRange && textObj.caretPos) {
                    var caretPos = textObj.caretPos;
                    caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == '' ? textFeildValue + '' : 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;
                    textObj.focus();
                    var len = textFeildValue.length;
                    textObj.setSelectionRange(rangeStart + len, rangeStart + len);
                    textObj.blur();
                } else {
                    textObj.value += textFeildValue;
                }
            }
        }
--------------------编程问答-------------------- 多谢楼主!
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,