È possibile utilizzare uno dei plug-in filigrana di jQuery che fanno proprio questo. Uso un plug-in per filigrana con il seguente codice
$.fn.watermark = function (c, t) {
var e = function (e) {
var i = $(this);
if (!i.val()) {
var w = t || i.attr('title'), $c = $($("<div />").append(i.clone()).html().replace(/type=\"?password\"?/, 'type="text"')).val(w).addClass(c);
i.replaceWith($c);
$c.focus(function() {
$c.replaceWith(i); setTimeout(function() { i.focus(); }, 1);
})
.change(function (e) {
i.val($c.val()); $c.val(w); i.val() && $c.replaceWith(i);
})
.closest('form').submit(function() {
$c.replaceWith(i);
});
}
};
return $(this).live('blur change', e).change();
};
richiamabile in jQuery impostando la classe della casella di testo di input per filigrana come questo
<input type="text" id="keyword" name="keyword" class="watermark" style="width: 250px"
title="Type keyword here" />
Il titolo è ciò che verrà visualizzato nella filigrana.
fonte
2011-01-22 21:54:20
L'esempio rimuove anche il testo inserito di recente nel campo di input, che non è quello che si vorrebbe. – webtweakers