Use jquery to obtain the focus and defocus of the text box.
I still like to use jquery to implement it. No matter how many input boxes on the page need to be focused or defocused, they are all public. My common methods are as follows:
Traverse the input box on the page to obtain the val value in the input box. When the input box is focused, compare it with the stored oldValue value. If the value is the same, the value is changed to null, in case of defocus, the original stored value is assigned to val. The Code is as follows:
$(function(){ $("input[class*=input]").each(function(){ var oldValue=$(this).val(); $(this).focus(function(){ if($(this).val()==oldValue){ $(this).val(''); } }) .blur(function(){ if($(this).val()==""){ $(this).val(oldValue) } }) });})
Js Implementation Method 1: directly add
<Input type = "text" onfocus = "if (this. value = 'focus bar') this. value = '';" onblur = "if (this. value = '') this. value = 'focus Ba '; "value =" Focus ba "name =" name ">
Js Implementation Method 2:
Html code:
<Input type = "text" value = "Focus bar" id = "myinput"/>
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 there is a focus on the page and the focus is deprecated, I recommend using jquery.