<< Sharp Jquery>> Example improved use, HTML text input box get with the loss of input focus information display toggle function

Source: Internet
Author: User
Tags cdata

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.