I still like to use jquery to achieve, no matter how many input boxes on the page need to focus, lose focus, all public, my usual method is:
Traversing the input box on the page, get the Val value in the box, and when the input box is focused, compare it to the stored oldvalue value, and if the value is the same, change the value to empty, and when it loses focus, the original stored value is then assigned to Val, the code is as follows:
$ (function () { $ ("input[class*=input]"). each (function () { var oldvalue=$ (this). Val (); $ (this). focus (function () { if ($ (). Val () ==oldvalue) { $ (this). Val ("); } }) . Blur (function () { if ($ (this). val () = = "") { $ (this). Val (OldValue)}) })
JS Implementation method One: Directly in input to add
<input type= "text" onfocus= "if (this.value== ' Focus bar ') this.value="; "onblur=" if (this.value== ') this.value= ' focus '; " Value= "Spotlight" Name= "name" >
JS Implementation Method Two:
HTML code :
The JS code is as follows:
function AddListener (ELEMENT,E,FN) { if (element.addeventlistener) { element.addeventlistener (e,fn,false); } else{ element.attachevent ("on" + E,FN); } } var myinput = document.getElementById ("Myinput"); AddListener (Myinput, "click", Function () { myinput.value = ""; }) AddListener (Myinput, "blur", function () { Myinput.value = "Focus"; })
Of course, when the page is focused and out of focus, I recommend using jquery for this implementation.
Get the focus and defocus method of a text box with jquery