In the second edition of the book 3.2.101, the introduction of the Val () method, the sample code used is common, and the example of the scene mode in the actual work will be used, so try to optimize a bit, write an HTML text input box to get with the loss of input focus of the prompt information display switch function.
Original Book Code:
<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional . DTD "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><title>3-10-2-2</title> <!--Introducing jquery - <Scriptsrc=".. /.. /scripts/jquery.js "type= "Text/javascript"></Script> <Scripttype= "Text/javascript"> //<! [Cdata[ $(function(){ $("#address"). Focus (function(){ //Address box get mouse focus varTxt_value= $( This). Val (); //get the value of the current text box if(Txt_value== This. DefaultValue) { $( This). Val (""); //empty the contents of the text box if the condition is met } }); $("#address"). Blur (function(){ //Address box loses mouse focus varTxt_value= $( This). Val (); //get the value of the current text box if(Txt_value==""){ $( This). Val ( This. DefaultValue);//If the condition is met, the content is set } }) $("#password"). Focus (function(){ varTxt_value= $( This). Val (); if(Txt_value== This. DefaultValue) { $( This). Val (""); } }); $("#password"). Blur (function(){ varTxt_value= $( This). Val (); if(Txt_value==""){ $( This). Val ( This. DefaultValue); } }) }); //]]> </Script></Head><Body> <inputtype= "text"ID= "Address"value= "Please enter your email address"/> <BR/><BR/> <inputtype= "text"ID= "Password"value= "Please enter your email password"/> <BR/><BR/> <inputtype= "button"value= "Login"/></Body></HTML>
Change the JavaScript code in one of the following sections:
<Scripttype= "Text/javascript"> //<! [Cdata[ $(function(){ varToggleinputtiponfocusblur= function(event) {//text box gets mouse focus if(Event.type=="Focus" && $( This). Val ()== This. DefaultValue) { $( This). Val (""); //clears the contents of the text box if it matches the condition of the input box content as a cue message } Else if(Event.type=="Blur" && $( This). Val ()==""){ $( This). Val ( This. DefaultValue);//If the entry is empty, the content is set } }; $("#address"). focus (Toggleinputtiponfocusblur). blur (Toggleinputtiponfocusblur); $("#password"). focus (Toggleinputtiponfocusblur). blur (Toggleinputtiponfocusblur); }); //]]> </Script>
With function chain and fixed function name Toggleinputtiponfocusblur, almost no brain copy code can be set up.
One of the main points: event handler gets the event parameter, which can be obtained by its Type property.
<< Sharp Jquery>> Example improved use, HTML text input box get with the loss of input focus information display toggle function