ugrás a tartalomhoz

Adding HTML5 placeholder attribute support through progressive enhancement

Joó Ádám · 2010. Jún. 23. (Sze), 09.42
Hogyan vegyük használatba a placeholder attribútumot
 
1

jQuery plugin

Poetro · 2010. Jún. 23. (Sze), 18.42
Erre én írtam egy valamivel szebben konfigurálható jQuery plugint:
  1. (function ($) {  
  2.   $.fn.extend({  
  3.     /** 
  4.      * Placeholders for input elements which hide on focus. 
  5.      * 
  6.      * Uses the current value as the placeholder, 
  7.      * unless overriden by `useAttribute`. 
  8.      * 
  9.      * Usage: 
  10.      *  $('input').inputPlaceholder({ 
  11.      *    'blurClass' : 'field-blur', 
  12.      *    'focusClass' : 'field-focus', 
  13.      *    'clearOnSubmit' : true, 
  14.      *    'useAttribute: 'title' 
  15.      *  }); 
  16.      * 
  17.      * @param settings Object 
  18.      *   Set the following behaviors: 
  19.      *    - `blurClass` : class to be added to the field on blur (default is ''). 
  20.      *    - `focusClass` : class to be added to the field on focus (default is ''). 
  21.      *    - `clearOnSubmit` : clear the field value, if contains default value. 
  22.      *    - `useAttribute` : use an attribute of the field, instead of the 
  23.      *                       current value for the placeholder (default is false). 
  24.      * @returns jQuery object of the original selection for chaining. 
  25.      */  
  26.     inputPlaceholder : function (settings) {  
  27.         // Only use the INPUT type TEXT and PASSWORD  
  28.         var inputs = $(this).filter(':text, :password');  
  29.   
  30.         // Focus and blur classes.  
  31.         settings = $.extend({  
  32.             'blurClass'    : '',  
  33.             'focusClass'   : '',  
  34.             'clearOnSubmit'false,  
  35.             'useAttribute' : false  
  36.           }, settings);  
  37.   
  38.         if (inputs.length) {  
  39.           inputs.each(function () {  
  40.             var $this = $(this),  
  41.                 placeholder = settings.useAttribute ?  
  42.                   this[useAttribute] || '' :  
  43.                   $this.val();  
  44.   
  45.             $this.bind('focus.inputPlaceholder blur.inputPlaceholder'function(event) {  
  46.                 // Store current value, to be used later on.  
  47.                 var fieldValue = $this.val();  
  48.   
  49.                 switch (event.type) {  
  50.                   case 'focus':  
  51.                     // Field's value is the same as the default, so make it empty  
  52.                     if (placeholder === fieldValue) {  
  53.                       $this.val('').removeClass(settings.blurClass).addClass(settings.focusClass);  
  54.                     }  
  55.                     break;  
  56.   
  57.                   case 'blur':  
  58.                     // Field value is empty so fill it with default.  
  59.                     if (fieldValue === '') {  
  60.                       $this.val(placeholder).removeClass(settings.focusClass).addClass(settings.blurClass);  
  61.                     }  
  62.                     break;  
  63.                 }  
  64.               });  
  65.   
  66.           }).addClass(settings.blurClass);  
  67.   
  68.           // Should it clear the default value on submit?  
  69.           if (settings.clearOnSubmit) {  
  70.             inputs.parents('form:first')  
  71.               .bind('submit.inputPlaceholder'function () {  
  72.                 inputs.each(function () {  
  73.                   // Have to go through each,  
  74.                   // as triggerHandler only triggers for first item  
  75.                   $(this).triggerHandler('focus.inputPlaceholder');  
  76.                 })  
  77.               });  
  78.           }  
  79.         }  
  80.   
  81.         return this;  
  82.       }  
  83.   });  
  84. }(jQuery));  
És ezután csak
  1. if (!('placeholder' in document.createElement('input'))) {  
  2.   $('input:text[placeholder]').inputPlaceholder({'useAttribute''placeholder'});  
  3. }  
2

Nagyon tetszik

N0r3i · 2010. Jún. 27. (V), 12.18
Szia!

Pont ilyesmit keresgéltem a saját, kicsit béna megoldásom helyett.

Egy kis baki van azért benne: a 43. sorban a this helyett $this kell, gondolom csak elírás... Így már tökéletesen működik, köszönöm!

Norbi
3

Javított változat

Poetro · 2010. Jún. 27. (V), 17.08
Enyhén optimalizált, rövidebb javított változat:
  1. (function ($) {    
  2.   $.fn.extend({    
  3.     /** 
  4.      * Placeholders for input elements which hide on focus. 
  5.      * 
  6.      * Uses the current value as the placeholder, 
  7.      * unless overriden by `useAttribute`. 
  8.      * 
  9.      * Usage: 
  10.      *  $('input').inputPlaceholder({ 
  11.      *    'blurClass' : 'field-blur', 
  12.      *    'focusClass' : 'field-focus', 
  13.      *    'clearOnSubmit' : true, 
  14.      *    'useAttribute: 'title' 
  15.      *  }); 
  16.      * 
  17.      * @param settings Object 
  18.      *   Set the following behaviors: 
  19.      *    - `blurClass` : class to be added to the field on blur (default is ''). 
  20.      *    - `focusClass` : class to be added to the field on focus (default is ''). 
  21.      *    - `clearOnSubmit` : clear the field value, if contains default value. 
  22.      *    - `useAttribute` : use an attribute of the field, instead of the 
  23.      *                       current value for the placeholder (default is false). 
  24.      * @returns jQuery object of the original selection for chaining. 
  25.      */  
  26.     inputPlaceholder : function (settings) {  
  27.       // Only use the INPUT type TEXT and PASSWORD  
  28.       var inputs = $(this).filter('input:text, input:password'),  
  29.           focus = 'focus.inputPlaceholder',  
  30.           blur = 'blur.inputPlaceholder',  
  31.           clearValues;  
  32.   
  33.       // Focus and blur classes.  
  34.       settings = $.extend({  
  35.           'blurClass'    : '',  
  36.           'focusClass'   : '',  
  37.           'clearOnSubmit'false,  
  38.           'useAttribute' : false  
  39.         }, settings);  
  40.   
  41.       if (inputs.length) {  
  42.         inputs.each(function () {  
  43.           var field = $(this),  
  44.               placeholder = settings.useAttribute ?  
  45.                 field.attr(settings.useAttribute) :  
  46.                 field.val();  
  47.   
  48.           field.bind(focus, function(event) {  
  49.               // Store current value, to be used later on.  
  50.               var fieldValue = field.val();  
  51.   
  52.               // Field's value is the same as the default, so make it empty  
  53.               if (placeholder === fieldValue) {  
  54.                 field.val('').removeClass(settings.blurClass).addClass(settings.focusClass);  
  55.               }  
  56.             }).bind(blur, function () {  
  57.               // Store current value, to be used later on.  
  58.               var fieldValue = field.val();  
  59.   
  60.               // Field value is empty so fill it with default.  
  61.               if (!fieldValue) {  
  62.                 field.val(placeholder).removeClass(settings.focusClass).addClass(settings.blurClass);  
  63.               }  
  64.             }).triggerHandler(blur);  
  65.         });  
  66.   
  67.         // Should it clear the default value on submit?  
  68.         if (settings.clearOnSubmit) {  
  69.           clearValues = function () {  
  70.             inputs.each(function () {  
  71.               // Have to go through each,  
  72.               // as triggerHandler only triggers for first item  
  73.               $(this).triggerHandler(focus);  
  74.             });  
  75.           };  
  76.           inputs.parents('form:first').bind('submit.inputPlaceholder', clearValues);  
  77.           $(window).bind('unload.inputPlaceholder', clearValues);  
  78.         }  
  79.       }  
  80.   
  81.       return this;  
  82.     }  
  83.   });    
  84. }(jQuery));