Text Box get focus and lose focus

Source: Internet
Author: User

Today, I would like to introduce two ways to get focus and lose focus in the text box:

First: html5

Html5 adds several new attributes to the form text box, such as email, tel, number, time, required, autofocus, and placeholder. These attributes greatly change the form effect.

Placeholder is one of them, which can get the focus and lose the focus in the text box at the same time. Make sure that the value of input is null. The content of placeholder is what we see on the page.

The Code is as follows:

<Input type = "text" value = "" placeholder = "Enter the content"/>

 

Type 2: jQuery

Principle: Make the val value of the form equal to the title value.

The Code is as follows:

<Input type = "text" value = "" title = "Enter content"/>

 

1 <script type = "text/javascript">
2 $ (function (){
3 var $ input = $ ("input ");
4
5 $ input. each (function (){
6 var $ title = $ (this). attr ("title ");
7
8 $ (this). val ($ title );
9
10 $ (this). focus (function (){
11 if ($ (this). val () ===$ title ){
12 $ (this). val ('');
13}
14 })
15
16. blur (function (){
17 if ($ (this). val () = ""){
18 $ (this). val ($ title );
19}
20 });
21 });
22 });
23 </script>

 

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.