text box get focus and lose focus Js/jquery method

Source: Internet
Author: User

First of all, JavaScript is written directly on the input

The code is as follows Copy Code

<input name= "Pwuser" type= "text" id= "Pwuser" class= "input" value= "real Estate account" onblur= "if (this.value== ') this.value= ' real estate Account Number '; "onfocus=" if (this.value== ' real estate account ') this.value= '; "/>
<input name= "pwpwd" type= "password" class= "input1" "value=" "Hu" "onblur=" if (this.value== ') this.value= ' Hu Jintao '; Onfocus= "if (this.value== ') this.value= ';" >


jquery Implementation Method

For the element focus event, we can use the focus function of jquery (), Blur ().

Focus (): used in the same way as onfocus in JavaScript, when it is focused.
Such as:

The code is as follows Copy Code
$ ("P"). focus (); or $ ("P"). Focus (FN)

Blur (): Use when losing focus, same as onblur.
Such as:

The code is as follows Copy Code
$ ("P"). blur (); or $ ("P"). blur (FN)

Instance

The code is as follows Copy Code

<form>
<label for= "Searchkey" id= "Lbsearch" > Search god Horse? </label> here the label is overlaid on the text box to better control the style
<input id= "Searchkey" type= "text"/>
<input type= "Submit" value= "Search"/>
</form>

jquery Code

The code is as follows Copy Code

$ (function () {
$ (' #searchKey '). focus (function () {
$ (' #lbSearch '). Text (');
});
$ (' #searchKey '). blur (function () {
var str = $ (this). Val ();
str = $.trim (str);
if (str = = ")
$ (' #lbSearch '). Text (' Search God horse? ');
});
})

Well, that's pretty good.

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.