Editable DIV Positions the cursor to the last

Source: Internet
Author: User

Recently made a can reply to the message page, mainly including: @, the expression function, a beginning to use the text field textarea, and finally found that the expression can not be displayed, and later changed to editable Div, add contenteditable= "true" in the Div , then the div can be edited.

<contenteditable= "true"></div>

After choosing an expression or selecting a person, you will often find that the cursor in the editable div disappears, or the location is incorrect, so I found some methods on the Internet, and now summarize the following:

functionSet_focus () {El=document.getelementbyid (' Guestbook_editor '); //El=el[0]; jquery Object goto DOM ObjectEl.focus (); if($.support.msie) {varRange =Document.selection.createRange ();  This. Last =range;        Range.movetoelementtext (EL);        Range.Select (); Document.selection.empty (); //uncheck    }    Else    {        varRange =Document.createrange ();        Range.selectnodecontents (EL); Range.collapse (false); varSEL =window.getselection ();        Sel.removeallranges ();    Sel.addrange (range); }}

Call this method to place the cursor at the end of an editable div.

Reference Address: http://chwjbn.blog.163.com/blog/static/1038799201310762620681/

Editable DIV Positions the cursor to the last

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.