Make a text box that can be filled in with JavaScript (2)

zhaozj2021-02-16  56

Copyright Notice: You can reprint anything, please be sure to indicate the original source of the article by hyperlink http://xinyistudio.vicp.net/ and author information and this statement

(Connected to above)

Set the text box focus at the last end of the function so that the user knocked into the character can replace the selected text.

Function TextBoxSelect (OTextBox, iStart, IEND) {Switch (arguments.length) {casse 1: OtextBox.select (); break; case 2: ip = textbox.value.length; / * falls through * / casse 3: IF ( ISIE) {var Orange = OtExtBox.createTextRange (); Orange.MoveStart ("Character", iStart); Orange.Movend ("Character", -OxtEndBox.value.Length Iend); Orange.Select ();} else (ismoz) {OTextBox.SetSelectionRange (iStart, IEND);}}}}}}}

Ø Replace the text selected in the text box (Translator Note: This feature seems to be inappropriate, readers directly skip)

-----------

Another task is that we need to replace the currently selected text with some other text. To do this, create a TextBoxReplaceElectr () method, which has two parameters: the text box object that is acting and the text to be inserted. Here we want to use the TextRange object, we need to build different code for IE and Mozilla, let us give the IE code:

function textboxReplaceSelect (oTextbox, sText) {if (isIE) {var oRange = document.selection.createRange (); oRange.text = sText; oRange.collapse (true); oRange.select ();} else if (isMoz) { }} ▲xtbox.focus ();

On the 4th line above, a TextRange object is created from the Document Current Selection Area (which can be imagined in the text box, trigger by the onkeypress event), and then the subsequent line we replace the text in the selection range with a given string. In line 6, the collapse method of the TextRange object is called, and the width of the selection range is set to zero; where the Boolean parameter is TRUE, the cursor is biased toward the end of the selection range, which is reversed. Finally, we use the select () method to determine the cursor at the end of the selection range. Use some simple operations of the String object in the Mozilla browser to complete the same effect. We can use the SelectStart and SelectEnd properties of the text box object to determine the start and end points of the selection:

function textboxReplaceSelect (oTextbox, sText) {if (isIE) {var oRange = document.selection.createRange (); oRange.text = sText; oRange.collapse (true); oRange.select ();} else if (isMoz) { var iStart = oTextbox.selectionStart; oTextbox.value = oTextbox.value.substring (0, iStart) sText oTextbox.value.substring (oTextbox.selectionEnd, oTextbox.value.length); oTextbox.setSelectionRange (iStart sText.length iStart Stext.length;} OTextBox.focus ();

On the 9th line above, the selected starting point is saved, and the 10th line looks quite puzzled. In fact, this line code is just before the selected text, and the subsequent string is replaced between them. String text.

The next sentence is to correct the length of the cursor after the location of the text - the start point position plus the length of the text.

Ø match

---------

The next step is to handle a way to write a method: a given string A. Search in a string array and return the first string value B using this string A (for example: by 'a') Find the string value "ABCD" of "A" in the STRING array. The name of this method is AutoCompleteMatch (), which has two parameters: a number of texts that need to match the text and all text values ​​that are matched.

Direct Search - Repeatedly checks each value in the array. Method Returns a value in the form of TRUE, otherwise it will return NULL if there is no match. All code is as follows:

Function AutoCompleteMatch (Stext, Arrvalues) {for (var i = 0; I

It should be noted that: to ensure that this function works correctly, you should sort the strings in the array.

(one two Three)

转载请注明原文地址:https://www.9cbs.com/read-24893.html

New Post(0)