A friendly text box shows the prompt jquery plug-in, text box jquery
The plug-in displays prompts in the text box by default. When the text box gets the focus, the prompt disappears. If there is no input or the input is null, the prompt appears again when the focus is lost.
At the same time, it is very comfortable and simple to use. After the plug-in and jquery are introduced, style classes (class = "prompt-input") are added to the original text box ") and set the value (value = "Your prompt") as the prompt.
Like this:
1 <input class="prompt-input" type="text" value='Your prompt' />
At the same time, the method of obtaining the value does not need to be changed. In this way, adding the plug-in effect to the completed project does not require any change.
Implement js:
1/* 2*3 * Version: 1.2.0 4 * Author: jinglan. woo (a) gmail.com 5 * Date: 2014.08.07 6*7 * Friendly prompt text input box: 8 * the text input box has focus prompt disappears 9 * and prompt appears again when it out of focus10 * 11 */12 13 (function ($) {14 $. fn. promptInput = function (prompt, fontColor) {15 var $ this = $ (this); // The current input text box 16 prompt = prompt? Prompt: $ this. val (); // prompt 17 fontColor = fontColor in the input box? FontColor: '# ccc'; // color of the prompt language 18 19 var $ promptInput = $ this. clone (); // clone the input text box to display 20 21 rows promptinput.addclass('prompt-input'0000.css ('color', fontColor) 22. attr ('propt', prompt ). attr ('type', 'text '). removeAttr ('name '). removeAttr ('id') 23. val (prompt); // instantiate the displayed text box 24 25 $ promptInput26. focusin (function () {// when getting the focus, remove the prompt 27 rows (this).css ('color', ''); 28 if ($ (this ). val () ==$ (this ). attr ('propt') {29 $ (this ). val (''); 30} 31}) 32. focusout (function () {// when the focus is lost, a prompt 33 if ($ (this) is displayed ). val (). replace (/\ s/g, '') ='') {34 values (this%.val(%(this%.attr('prompt'%}.css ('color', fontColor); 35 $ (this ). next (). val (''); 36} 37 }). change (function () {// when the value changes, a value of 38 $ (this) is assigned to the currently passed text box ). next (). val ($ (this ). val (); 39}); 40 41 $ this. attr ('type', 'ddn '). val (''); // change the type of the currently passed text box to hide the domain 42 $ promptInput. insertBefore ($ this); // append the cloned object to the page 43}; 44}) (jQuery); 45 46 $ (function () {47 $ ('. prompt-input '). each (function (index, element) {// page loading completes automatic detection. prompt-input class, loading effect 48 $ (element ). promptInput (); 49}); 50 });Jquery. promptInput. js
Use html:
1 <! DOCTYPE html> 2
You can copy the above Code to run it directly, or download the complete code and demo here.
How does jquery obtain any text box in a set of text boxes?
<Script src = "jquery-1.6.4.min.js"> </script>
<Script>
$ (Function (){
$ (". D> input [type = 'text']"). click (function (){
Alert ($ (this). val ());
})
})
</Script>
<Div class = "d">
<Input type = "text" value = "test1"/>
<Input type = "text" value = "test2"/>
<Input type = "text" value = "test3"/>
</Div>
Written in Jquery: If a button is clicked next to a text box, 0 is displayed for the first time, 1 is displayed for the second time, 2 is displayed for the third time, and then 2 is clicked.
<Script type = "text/javascript" src = "scripts/jquery-1.7.1.min.js"> </script>
<Script language = "javascript">
$ (Document). ready (function (){
Var I = 0;
$ ('# Butt'). click (
Function (){
$ ('# Txt1'). val (I );
I = I + 1;
Return I;
}
);
});
</Script>
<Input type = "text" id = "txt1"> <input type = "button" id = "butt" value = "">