Jquery obtains the focus Event code and loses the focus event.

Source: Internet
Author: User

The input loses focus and obtains focus.
The text in the search box disappears.
When we are doing a website, we often use the search box to get the focus and to lose the focus. because of laziness, every time we write it, it's annoying, in a similar situation, just call it.

Related js Code:
Copy codeThe Code is as follows:
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> input loses focus and jquery focus event Plugin-Lazy website construction </title>
<Script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
// Focusblur
JQuery. focusblur = function (focusid ){
Var focusblurid = $ (focusid );
Var defval = focusblurid. val ();
Focusblurid. focus (function (){
Var thisval = $ (this). val ();
If (thisval = defval ){
$ (This). val ("");
}
});
Focusblurid. blur (function (){
Var thisval = $ (this). val ();
If (thisval = ""){
$ (This). val (defval );
}
});
};
/* The following is the call Method */
$. Focusblur ("# searchkey ");
});
</Script>
</Head>
<Body>
<Form action = "" method = "post">
<Input name = "" type = "text" value = "input search keyword" id = "searchkey"/>
<Input name = "" type = "submit" id = "searchbtn" value = "Search"/>
</Form>
<P> the input loses focus and obtains focus jquery focus event plug-in. <br/> <strong style = "color: # F00 "> the text disappears when you click in the search box </strong>. </P>
</Body>
</Html>

Jquery gets and loses focus events
Copy codeThe Code is as follows:
<Script src = "jquery-1.9.1.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
$ (Function (){
$ ('# Username'). focus (function () // obtain the trigger time of the classroom.
{
$ ('# Username'). val ('');
})
$ ('# Username'). Time triggered when blur (function () loses focus
{
If ($ ('# username'). val () = 'marry '){
$ ('# Q'). text ('user name already exists! ')
}
Else {$ ('# Q'). text (' OK! ')}
})

Related Article

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.