Insert content in the jQuery-based input box at the cursor position, and select _ jquery

Source: Internet
Author: User
The jQuery-based input box inserts content at the cursor position and selects the implementation code of the function. For more information, see. The Code is as follows:


//
// Usage
// $ (Text field selector). insertContent ("inserted content ");
// $ (Text field selector ). insertContent ("inserted content", numeric value); // select the boundary between the two sides of the inserted text content based on the value. The value 0 indicates that all the inserted text is selected, -1 indicates that only one character is selected for each inserted text.
//
// Insert content at the cursor position and select
(Function ($ ){
$. Fn. extend ({
InsertContent: function (myValue, t ){
Var $ t = $ (this) [0];
If (document. selection) {// ie
This. focus ();
Var sel = document. selection. createRange ();
Sel. text = myValue;
This. focus ();
Sel. moveStart ('character ',-l );
Var wee = sel. text. length;
If (arguments. length = 2 ){
Var l = $ t. value. length;
Sel. moveEnd ("character", wee + t );
T <= 0? Sel. moveStart ("character", wee-2 * t-myValue. length): sel. moveStart ("character", wee-t-myValue. length );
Sel. select ();
}
} Else if ($ t. selectionStart | $ t. selectionStart = '0 '){
Var startPos = $ t. selectionStart;
Var endPos = $ t. selectionEnd;
Var scrollTop = $ t. scrollTop;
$ T. value = $ t. value. substring (0, startPos) + myValue + $ t. value. substring (endPos, $ t. value. length );
This. focus ();
$ T. selectionStart = startPos + myValue. length;
$ T. selectionEnd = startPos + myValue. length;
$ T. scrollTop = scrollTop;
If (arguments. length = 2 ){
$ T. setSelectionRange (startPos-t, $ t. selectionEnd + t );
This. focus ();
}
}
Else {
This. value + = myValue;
This. focus ();
}
}
})
}) (JQuery );

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.