How to calibrate the implementation method on the far right of the input box is required by many people. This article will introduce in detail. For more information, see
This scenario is often required during front-end development. Use JS to calibrate the light on the rightmost side of the input box.
Scenario 1: edit the description of an image
Scenario 2: Script. aculo. us Ajax. InPlaceEditor class. You can double-click it to edit it. You can automatically update the region after editing it.
All of the above scenarios require JS to achieve optical calibration at the far right of the input box, but not by clicking the mouse into the input box.
We know that the most basic method for implementation is the focus Method of HTMLElement. As follows:
The Code is as follows:
Script
Var input = document. getElementsByTagName ('input') [0];
Input. focus ();
Script
Open this page and you will find that the cursor is at the far left of the input box. The effect is as follows:
Now we need to implement three steps to set the optical calibration to the far right of the input box.
1. Call the focus method.
2. The value assignment is null.
3. Assign the previous input value to yourself.
The Code is as follows:
Script
Var input = document. getElementsByTagName ('input') [0];
Var val = input. value;
Input. focus ();
Input. value = '';
Input. value = val;
Script
After running, the cursor is on the rightmost side of the in-depth box.