A simple example of the gain and loss focus of the JQuery text box _jquery

Source: Internet
Author: User

Version One

CSS Code section:

Copy Code code as follows:

. focus {
border:1px solid #f00;
Background: #fcc;
}

Add focus style, add border, and change background color to #fcc when focal point is obtained

HTML code section:

Copy Code code as follows:

<body>
<form action= "" method= "Post" id= "RegForm" >
<fieldset>
<legend> Personal basic Information </legend>
<div>
<label for= "username" > name:</label>
<input id= "username" type= "text"/>
</div>
<div>
<label for= "pass" > Password:</label>
<input id= "pass" type= "password"/>
</div>
<div>
<label for= "MSG" > Detailed information:</label>
<textarea id= "MSG" rows= "2" cols= "></textarea>"
</div>
</fieldset>
</form>
</body>

Here are two input, a textare box.

: Input matches all input, textarea, select, and button elements.

jquery Code section:

Copy Code code as follows:

<script type= "Text/javascript" >
$ (function () {
$ (": Input"). focus (function () {
$ (this). AddClass ("Focus");
}). blur (function () {
$ (this). Removeclass ("Focus");
});
})
</script>

Use: Input matches all input elements, and when the focus is obtained, the style focuses are added, and the current element is automatically identified by $ (this). The focus () method is the function that is taken when a focused event occurs. The blur () method is the function that is executed when the focus event occurs.

Version two:

Sometimes the text box has the default content, as a cue message, to get the focus, to let it disappear. Can do the following transformation:

Copy Code code as follows:

<script type= "Text/javascript" >
$ (function () {
$ (": Input"). focus (function () {
$ (this). AddClass ("Focus");
if ($ (this). Val () ==this.defaultvalue) {
$ (this). Val ("");
}
}). blur (function () {
$ (this). Removeclass ("Focus");
if ($ (this). val () = = ') {
$ (this). Val (This.defaultvalue);
}
});
})
</script>

Make a logical decision to empty the contents of the text box if the value is the default value.

Loses focus, if the text box is empty, that is, there is no input, the value is also set to the default value.

This is a simple logic.

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.