JavaScript gets the focus on the back of the last text in the text box implement code _javascript Tips

Source: Internet
Author: User
Copy Code code as follows:

<script>
When the intersection is lost, let the text in the TextBox get the focus and the cursor moves behind the last word.
function Myfocus (myID) {
if (Isnav) {
document.getElementById (myID). focus ();//Get focused

}else{
Setfocus.call (document.getElementById (myID));
}
}
var Isnav = (Window.navigator.appName.toLowerCase (). IndexOf ("Netscape") >=0);
var Isie = (Window.navigator.appName.toLowerCase (). IndexOf ("Microsoft") >=0);
function SetFocus () {
var range = This.createtextrange (); Create a text selection
Range.movestart (' character ', this.value.length); The beginning of the selection moves to the last
Range.collapse (TRUE);
Range.Select ();
}
</script>

Example:

This is a fuzzy search with Ztree.
At first all the tree nodes are closed, but if the input search content when the query, expand the query content of the parent node, will make the input inside the loss of focus, the user experience is not good, here in the parent node, and then let input gain focus, if only the use of focusing () , the cursor appears at the front of the text box. Of course, this is not the effect I want. Call the above code, you can implement after the query, the cursor positioned behind the query text.

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.