使用jQuery清除焦点输入并返回模糊 [英] Clear input on focus with jQuery and return on blur
本文介绍了使用jQuery清除焦点输入并返回模糊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这几乎可行.但是,当离开该字段时,将显示"defaulttext"而不是原始文本值.不确定如何最有效地回显defaultText内部的变量.
This almost works. However, when leaving the field "defaulttext" appears rather than the original text value. Not sure how to most efficiently echo the variable inside defaultText.
$(function() {
var defaultText = $(this).val();
$('input[type=text]').focus(function() {
$(this).val('');
});
$('input[type=text]').blur(function() {
$(this).val('defaultText');
echo
});
});
推荐答案
$(function() {
var input = $('input[type=text]');
input.focus(function() {
$(this).val('');
}).blur(function() {
var el = $(this);
/* use the elements title attribute to store the
default text - or the new HTML5 standard of using
the 'data-' prefix i.e.: data-default="some default" */
if(el.val() == '')
el.val(el.attr('title'));
});
});
更新
浏览器正在逐步实现placeholder
属性,该属性提供了向用户显示提示"的功能.
Browsers are progressively implementing the placeholder
attribute, which provides the ability to display a "hint" to the user.
https://developer.mozilla. org/zh-CN/docs/Web/HTML/Element/Input#attr-placeholder
这篇关于使用jQuery清除焦点输入并返回模糊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文