अपडेट: मैंने आपकी कहानियां सचमुच "क्लिक" ली, जो मेरे बारे में थोड़ा गूंगा था। आप नीचे दिए गए के सभी में click
के लिए focus
स्थानापन्न कर सकते हैं अगर आप भी कार्रवाई चाहते हो जब इनपुट है, जो संभावना है करने के लिए उपयोगकर्ता टैब।
अपडेट 2: मेरा अनुमान है कि आप प्लेसहोल्डर करना चाहते हैं; अंत में टिप्पणी और उदाहरण देखते हैं।
मूल जवाब:
आप ऐसा कर सकते हैं:
$("selector_for_the_input").click(function() {
this.value = '';
});
... लेकिन यह है कि यह क्या है की परवाह किए बिना पाठ साफ हो जाएगा। आप केवल यह साफ़ करना चाहते हैं, तो यह एक विशिष्ट मूल्य है:
$("selector_for_the_input").click(function() {
if (this.value === "TEXT") {
this.value = '';
}
});
उदाहरण के लिए, यदि इनपुट एक id
है, तुम कर सकते हो:
$("#theId").click(function() {
if (this.value === "TEXT") {
this.value = '';
}
});
या यह एक साथ एक के रूप में अगर id
(जैसे कि, "MyForm") और आप हर रूप क्षेत्र के लिए ऐसा करना चाहते हैं:
$("#myForm input").click(function() {
if (this.value === "TEXT") {
this.value = '';
}
});
तुम भी प्रतिनिधिमंडल के साथ यह करने के लिए सक्षम हो सकता है:
$("#myForm").delegate("input", "click", function() {
if (this.value === "TEXT") {
this.value = '';
}
});
यह delegate
का उपयोग फॉर्म पर एक हैंडलर को हुक करने के लिए करता है लेकिन प्रत्येक व्यक्तिगत इनपुट में हैंडलर को जोड़ने के बजाय इसे फ़ॉर्म पर इनपुट पर लागू करता है।
आप प्लेसहोल्डर करने का प्रयास कर रहे हैं, वहाँ है कि तुलना में यह करने के लिए अधिक है और आप एक अच्छा प्लग में यह करने के लिए खोजने के लिए कर सकते हैं।लेकिन यहाँ मूल बातें है:
HTML:
<form id='theForm'>
<label>Field 1:
<input type='text' value='provide a value for field 1'>
</label>
<br><label>Field 2:
<input type='text' value='provide a value for field 2'>
</label>
<br><label>Field 3:
<input type='text' value='provide a value for field 3'>
</label>
</form>
जावास्क्रिप्ट का उपयोग jQuery:
jQuery(function($) {
// Save the initial values of the inputs as placeholder text
$('#theForm input').attr("data-placeholdertext", function() {
return this.value;
});
// Hook up a handler to delete the placeholder text on focus,
// and put it back on blur
$('#theForm')
.delegate('input', 'focus', function() {
if (this.value === $(this).attr("data-placeholdertext")) {
this.value = '';
}
})
.delegate('input', 'blur', function() {
if (this.value.length == 0) {
this.value = $(this).attr("data-placeholdertext");
}
});
});
Live copy
बेशक, आप भी नए placeholder
attribute एचटीएमएल 5 से उपयोग कर सकते हैं और केवल करना उपरोक्त यदि आपका कोड उस ब्राउज़र पर चल रहा है जो इसका समर्थन नहीं करता है, तो इस मामले में आप ऊपर दिए गए तर्क को उलटा करना चाहते हैं:
HTML: jQuery के साथ
<form id='theForm'>
<label>Field 1:
<input type='text' placeholder='provide a value for field 1'>
</label>
<br><label>Field 2:
<input type='text' placeholder='provide a value for field 2'>
</label>
<br><label>Field 3:
<input type='text' placeholder='provide a value for field 3'>
</label>
</form>
जावास्क्रिप्ट: (। कुडोस placholder
feature-detection code के लिए diveintohtml5.ep.io को)
jQuery(function($) {
// Is placeholder supported?
if ('placeholder' in document.createElement('input')) {
// Yes, no need for us to do it
display("This browser supports automatic placeholders");
}
else {
// No, do it manually
display("Manual placeholders");
// Set the initial values of the inputs as placeholder text
$('#theForm input').val(function() {
if (this.value.length == 0) {
return $(this).attr('placeholder');
}
});
// Hook up a handler to delete the placeholder text on focus,
// and put it back on blur
$('#theForm')
.delegate('input', 'focus', function() {
if (this.value === $(this).attr("placeholder")) {
this.value = '';
}
})
.delegate('input', 'blur', function() {
if (this.value.length == 0) {
this.value = $(this).attr("placeholder");
}
});
}
function display(msg) {
$("<p>").html(msg).appendTo(document.body);
}
});
Live copy
मुझे लगता है कि वी में वैल कम मामला होना चाहिए। ;-) – scunliffe
यह होना चाहिए, आप बिल्कुल सही हैं। ... Dammit, आईफोन ... –
@ डेविड: एलओएल आपके आईफोन पर SO सवालों का जवाब। यह समर्पित है। –