Compatibility of placeholder Properties

Source: Internet
Author: User

Placeholder is a new property of HTML5, and when input or textarea is set, the content of the value is displayed as a gray hint in the text box, and the text box disappears when it gets the focus (or input).


However, in browsers that do not support HTML5, the placeholder property is not valid, for example, IE9 and the following are incompatible with this property. The following describes the processing of placeholder compatibility


Add the following script to the page

$ (function ()  {  //  If placeholder is not supported, use jquery to complete   if (!issupportplaceholder ())  {    //  iterates through all input objects,  except for the Password box     $ (' input '). Not ("input[ Type= ' password '). each (      function ()  {         var self = $ (This);        var  Val = self.attr ("placeholder");         input (Self, val) ;      }    );         /**//*  Special handling      * 1 for password boxes. Create a text box        * 2. Switch      */    $ (' input[type= ' when getting focus and losing focus Password "]). each (      function ()  {         var pwdfield    = $ (this);           var pwdval       = pwdfield.attr (' placeholder ');       &NBSP;&NBSP;&NBSP;&NBSP;VAR&NBSP;PWDID&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;=&NBSP;PWDFIELD.ATTR (' id ');           //  Rename the input ID as the original ID followed by 1         pwdfield.after (' <input id= '  + pwdid + ' 1 " type=" text " value= ' +pwdval+ '  autocomplete=" Off " />");           var pwdplaceholder = $ (' # '  + pwdId +  ' 1 ');           pwdplaceholder.show ();           pwdfield.hide ();                     pwdplaceholder.focus (function () {             pwdplaceholder.hide ();             Pwdfield.show ();             pwdfield.focus ();           });                     pwdfield.blur (function () {             if (Pwdfield.val ()  ==  ")  {               pwdplaceholder.show ();               pwdfield.hide ();             }           });         }    )   });//  determine if the browser supports placeholder attribute function  issupportplaceholder ()  {  var input = document.createelement (' input ');   return  ' placeholder '  in input;}  jquery Replacement placeholder processing function input (obj, val)  {  var  $input  =  obj;  var val = val;   $input. attr ({value:val});   $ Input.focus (function ()  {    if  ($input. Val ()  == val)  {       $ (This). attr ({value: "}");     }  }). blur (Function ()  {    if  ($input. Val ()  ==  "")  {             $ (This). attr ({value:val});     }  });



Reference: Placeholder Compatibility http://www.studyofnet.com/news/1022.html


Compatibility of placeholder Properties

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.