I used a Jquery plug-in to insert or replace the selected content in the position of the optical indicator (compatible with the browsers)

Source: Internet
Author: User

This is my demonstration in IE and Firefox:

I don't want to remanufacture the wheel, but because there are problems with several functions I have found on the internet, either ie insertion is abnormal or firexfox cannot achieve the expected results, I suspect that these programmers did not pass the test after coding, so I had to rewrite it myself.
First, you need to download and use jquery. You can go to jquery.com to download this file, and then introduce the following code:

If you want to publish this article, please indicate the location, without the right to publish the copyright, I do not like to see that kind of website my work does not indicate the person QQ9256114

(Function ($ ){
$. Fn. insert = function (_ m ){
Var _ o = $ (this). get (0 );
If ($. browser. msie ){
_ O. focus (); sel = document. selection. createRange (); sel. text = _ m; sel. select ();
} Else if (_ o. selectionStart | _ o. selectionStart = '0 '){
Var startPos = _ o. selectionStart; var endPos = _ o. selectionEnd; var restoreTop = _ o. scrollTop;
_ O. value = _ o. value. substring (0, startPos) + _ m + _ o. value. substring (endPos, _ o. value. length );
If (restoreTop> 0) {_ o. scrollTop = restoreTop ;}
_ O. focus (); _ o. selectionStart = startPos + _ m. length; _ o. selectionEnd = startPos + _ m. length;
}
}
}) (JQuery)

Easy to use:

<Input type = 'button 'value = 'insert' onclick = '$ ("# Content"). insert ("abc")'> <br>
<Textarea id = 'content' name = 'content' rows = '13' style = 'width: 100% '> too many rows 1 </textarea>


Related Article

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.