0 jquery
我有:
<input type="text" id="nome" value="Nome..." />
Run Code Online (Sandbox Code Playgroud)
我必须删除它,一次 ....当焦点将在输入文本上.
我必须使用jquery ...
谢谢
简单回答:
$('#nome').focus(function() {
$(this).val('');
});
Run Code Online (Sandbox Code Playgroud)
详细解答:
虽然上面应该有用,但它并不是很友好,因为它会在你输入内容之后清除字段.我通常做这样的事情:
$('input.clearonfocus').focus(function() {
if (!$(this).data('originalValue')) {
$(this).data('originalValue', $(this).val());
}
if ($(this).val() == $(this).data('originalValue')) {
$(this).val('');
}
}).blur(function(){
if ($(this).val() == '') {
$(this).val($(this).data('originalValue'));
}
});
Run Code Online (Sandbox Code Playgroud)
后一个示例的优点是它删除了焦点上的值,但仅在您未更改值时才执行此操作.