2010-02-07 10 views
6

मैं इनपुट फ़ील्ड्स text और textarea में उपयोगी टेक्स्ट (किसी भी फ्रेमवर्क उपयोग के बिना जावास्क्रिप्ट में) जोड़ना चाहता हूं लेकिन यह नहीं जानता कि इसकी क्या चीज़ है<इनपुट प्रकार = "टेक्स्ट"> सहायक (उपयोगकर्ता प्रकार के दौरान पाठ को फीका करता है) जावास्क्रिप्ट

me.com http://i48.tinypic.com/rrhvye.png

बुलाया के रूप में यह me.com में प्रयोग किया जाता है, लेकिन मैं मान जो सहायकों के रूप में उपयोग किया जाता है प्रस्तुत करना चाहते हैं न।

खराब अंग्रेजी के लिए खेद है।

धन्यवाद।

उत्तर

2

मैं पाया है कि इनपुट क्षेत्र में एक <label> और यह स्थिति का उपयोग करने के लिए है इस को हल करने के लिए सबसे अच्छा तरीका। यह आपको देता है:

  1. अधिक सौंदर्य स्वतंत्रता
  2. अपने पृष्ठ अर्थ
  3. आप शान से नीचा
  4. इनपुट मूल्य के रूप में टूलटिप प्रस्तुत करके समस्याएं पैदा नहीं करता है या प्रबंधन के बारे में चिंता करने की ज़रूरत के लिए अनुमति देता रहता है उस समस्या

यहां एक वेनिला संस्करण है क्योंकि आपने कोई ढांचा नहीं मांगा है। मार्कअप को बदलने की आवश्यकता नहीं है, लेकिन आपको अपनी जरूरतों के साथ काम करने के लिए सीएसएस को समायोजित करने की आवश्यकता हो सकती है।

HTML:

<html> 
<head> 
    <style> 
    label.magiclabel { 
     position: absolute; 
     padding: 2px; 
    } 
    label.magiclabel, 
    input.magiclabel { 
     width: 250px; 
    } 
    .hidden { display: none; } 
    </style> 

    <noscript> 
     <style> 
      /* Example of graceful degredation */ 
      label.magiclabel { 
       position: static; 
      } 
     </style> 
    </noscript> 
</head> 
<body> 
<label>This is not a magic label</label> 

<form> 
    <label class="magiclabel" for="input-1">Test input 1</label> 
    <input class="magiclabel" type="text" id="input-1" name="input_1" value=""> 

    <label class="magiclabel" for="input-2">Test input 2 (with default value)</label> 
    <input class="magiclabel" type="text" id="input-2" name="input_2" value="Default value"> 
</form> 

<script src="magiclabel.js"></script> 
</body> 
</html> 

वेनिला-magiclabel.js

(function() { 
    var oldOnload = typeof window.onload == "function" ? window.onload : function() {}; 

    window.onload = function() { 
     // Don't overwrite the old onload event, that's just rude 
     oldOnload(); 
     var labels = document.getElementsByTagName("label"); 

     for (var i in labels) { 
      if (
       // Not a real part of the container 
       !labels.hasOwnProperty(i) || 
       // Not marked as a magic label 
       !labels[i].className.match(/\bmagiclabel\b/i) || 
       // Doesn't have an associated element 
       !labels[i].getAttribute("for") 
      ) { continue; } 

      var associated = document.getElementById(labels[i].getAttribute("for")); 
      if (associated) { 
       new MagicLabel(labels[i], associated); 
      } 
     } 
    }; 
})(); 

var MagicLabel = function(label, input) { 
    this.label = label; 
    this.input = input; 

    this.hide = function() { 
     this.label.className += " hidden"; 
    }; 

    this.show = function() { 
     this.label.className = this.label.className.replace(/\bhidden\b/ig, ""); 
    }; 

    // If the field has something in it already, hide the label 
    if (this.input.value) { 
     this.hide(); 
    } 

    var self = this; 

    // Hide label when input receives focuse 
    this.input.onfocus = function() { 
     self.hide(); 
    }; 

    // Show label when input loses focus and doesn't have a value 
    this.input.onblur = function() { 
     if (self.input.value === "") { 
      self.show(); 
     } 
    }; 

    // Clicking on the label should cause input to be focused on since the `for` 
    // attribute is defined. This is just a safe guard for non-compliant browsers. 
    this.label.onclick = function() { 
     self.hide(); 
    }; 
}; 

Vanilla demo

आप देख सकते हैं, कोड के बारे में आधे window.onload में प्रारंभ में लपेटा जाता है। ढांचे का उपयोग करके इसे कम किया जा सकता है। JQuery का उपयोग कर एक संस्करण यहां दिया गया है:

$(function() { 
    $("label.magiclabel[for]").each(function(index, label) { 
     label = $(label); 
     var associated = $("#" + label.attr("for")); 

     if (associated.length) { 
      new MagicLabel(label, associated); 
     } 
    }); 
}); 

var MagicLabel = function(label, input) { 
    // If the field has something in it already, hide the label 
    if (input.val() !== "") { 
     label.addClass("hidden"); 
    } 

    label.click(function() { label.addClass("hidden"); }); 
    input.focus(function() { label.addClass("hidden"); }); 
    input.blur(function() { 
     if (input.val() === "") { 
      label.removeClass("hidden"); 
     } 
    }); 
}; 

jQuery demo। मार्कअप को बदलने की जरूरत नहीं है, लेकिन निश्चित रूप से आपको jQuery लाइब्रेरी को शामिल करने की आवश्यकता होगी।

+0

एक सवाल हम इसे अधिभार के बिना लोड कर सकते हैं? चूंकि मैं इस सहायक का उपयोग कर रहा हूं, यह AJAX की मदद से भरा हुआ है और मैं पहले से ही कुछ चीजों का उपयोग कर रहा हूं। – Shishant

3

इस प्रयास करें:

<input type="text" name="member_name" value="Member Name" onFocus="field_focus(this, 'Member Name');" onblur="field_blur(this, 'Member Name');" /> 

बेशक, पासवर्ड फ़ील्ड के लिए इनपुट प्रकार पासवर्ड बनाने के लिए चाहते हैं, तो यह पासवर्ड पाठ बॉक्स के लिए उपयोगी नहीं होगा।

तुम भी कार्यों में इस लपेट कर सकते हैं अगर आप एक से अधिक क्षेत्रों के साथ काम कर रहे हैं:

<script type="text/javascript"> 
    function field_focus(field, text) 
    { 
    if(field.value == text) 
    { 
     field.value = ''; 
    } 
    } 

    function field_blur(field, text) 
    { 
    if(field.value == '') 
    { 
     field.value = text; 
    } 
    } 

</script> 
+0

नमस्ते इस ठीक काम करता है लेकिन हम इस स्पष्ट जब प्रपत्र वैकल्पिक फ़ील्ड के लिए की तरह प्रस्तुत किया जाता है सकते हैं? क्या हम इसे फ़ंक्शन में लपेट सकते हैं? क्योंकि मेरे पास बहुत सारे इनपुट फ़ील्ड और टेक्स्टरेस हैं – Shishant

+0

आप इन टेक्स्ट बॉक्स को क्यों साफ़ करना चाहते हैं? यदि आप फॉर्म सबमिट करने से पहले उन्हें साफ़ करना चाहते हैं तो इन टेक्स्ट बॉक्स का कोई भी डेटा प्रसंस्करण स्क्रिप्ट में नहीं भेजा जाएगा। – Sarfraz

+0

मेरा मतलब है कि मान डिफ़ॉल्ट मान हैं, तो उन्हें सबमिट करें, मैं इसे एक उत्पाद विवरण फ़ॉर्म का उपयोग करूँगा और लॉगिन फॉर्म नहीं करूँगा, इसलिए बहुत सारे विकल्प फ़ील्ड हैं। – Shishant

9

सबसे आसान तरीका:

<input type=text placeholder="Member name"> 
+0

यह सबसे अच्छा और साफ उत्तर है, लेकिन इसके साथ एक मजबूत चेतावनी है जिसमें 'प्लेसहोल्डर' विशेषता केवल [HTML5] (http://diveintohtml5.info/forms.html#placeholder) का उपयोग करते समय उपलब्ध है जब आपके पृष्ठ के रूप में एन्कोडिंग। – tatlar

संबंधित मुद्दे