События onfocus
и onblur
работают на всех элементах и якорях формы, вы можете попробовать просто сделать input
a textarea
, и это сработает, но я бы посоветовал вам сделать привязку событий программно .
Примерно так:
var textarea = document.getElementById('textareaId'),
message = 'Click here to type';
textarea.value = message; // set default value
textarea.onfocus = textarea.onblur = function () {
if (this.value == '') {
this.value = message;
} else if (this.value == message) {
this.value = '';
}
};
Попробуйте приведенный выше пример здесь .
jQuery версия :
$(function () {
var message = 'Click here to type';
$('#textareaId').val(message); // set default value
$('#textareaId').bind('focus blur', function () {
var $el = $(this);
if ($el.val() == '') {
$el.val(message);
} else if ($el.val() == message) {
$el.val('');
}
});
});