Есть много плагинов для этого. Это мое собственное решение, которое я использую:
/**
* Yet another textbox hint plugin...
* Usage: $('.selector').hint();
* The title attribute of the input will be used as the watermark value.
*/
(function($) {
$.fn.hint = function() {
var hintedClass = 'hinted';
return this.each(function() {
var $field = $(this),
title = $field.attr('title'),
$form = $(this.form);
// bail out if there isn't a title attribute
if (!title) { return; }
// add/remove hint behavior
var addHint = function() {
$field.val() || $field.addClass(hintedClass).val(title);
},
removeHint = function() {
if ($field.hasClass(hintedClass) && $field.val() === title) {
$field.val('').removeClass(hintedClass);
}
};
// set our focus/blur handlers
$field.focus(removeHint).blur(addHint);
// if the field isn't already focused, add the hint now
$field.is(':focus') || addHint();
// form submission handling
$form.submit(removeHint);
});
};
})(jQuery);