Introduction to adding characters to the cursor of a text box in js

Source: Internet
Author: User

Copy codeThe Code is as follows:
<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 + "insert character" + eng. text;
}
</Script>
<Input id = "Text1" type = "text" value = "1235467890"/>
<Input id = "Button4" type = "button"
Value = "button" onclick = "return Button4_onclick (Text1)"/>

Copy codeThe Code is as follows:
<% @ 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);"> example: Forget Never subexample: asp.net </textarea>
<Br/>
<Input type = "text" name = "textfield" style = "width: 220px;" value = "insert FireFox"/>
<Br/>
<Input type = "button" value = "insert"
Onclick = "insertAtCaret(this.form.tar ea, this. form. textfield. value);"/>
</P>
</Form>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.