Use jquery to make input prompt content, compatible with IE8 above

Source: Internet
Author: User

We all know that HTML5 's input new attribute has placeholder= "", then this incompatible IE low version we can only write with the script.

First HTML creates a new input

<input type= "text" class= "input" value= "Please enter search content"/>

Then we introduce the corresponding JS library, then use jquery

1 <script src= "Js/jquery-1.8.3.min.js" ></script> 2     <script> 3         $ (". Input"). Bind ( {4             focus:function () {  5                 if (this.value = = This.defaultvalue) {  6                     this.value= "";  7                 }  8             },  9             blur:function () {Ten                 if (this.value = = "") {one                     this.value = This.defaultvalue;         ),     </script>

Simple, so that is OK, then this is the text of input, we want to password password also can display IE low version, we use the method is to use two input, a text default display, a password default hidden, When text gets focus password display, text hidden, no input content loses focus text display, password display, OK, nonsense don't say more, look at the code!

1 <!DOCTYPE HTML>2 <HTMLLang= "en">3 <Head>4     <MetaCharSet= "UTF-8" />5     <title>Password box hint</title>6 </Head>7 <Body>8     9     <inputtype= "text"value= "Login Password"class= "Show" >Ten     <inputtype= "Password"class= "Log_paw"style= "Display:none;"> One      A      -     <Scriptsrc= "Js/jquery-1.8.3.min.js"></Script> -     <Script> the         $('. Show'). Focus (function(){ -             varText_value= $( This). Val (); -             if(Text_value==  This. DefaultValue) { -                 $( This). Hide (); +                 $( This). Next ('Input.log_paw'). Show (). focus (); -             } +         }); A         $('Input.log_paw'). Blur (function() { at             varText_value= $( This). Val (); -             if(Text_value== "') { -                 $( This). Prev ('. Show'). Show (); -                 $( This). Hide (); -             } -         }); in     </Script> - </Body> to </HTML>

Well, the code is here, hope to help you!

Use jquery to make input prompt content, compatible with IE8 above

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.