我正在使用 Jquery 删除焦点上 HTML 输入中的默认值。
但是,如果没有输入任何内容,我希望默认值重新出现。
为了尝试做到这一点,我创建了:
$( "#contact input" ).each(function( index ) {
$(this).focus(function() {
var valvalue = $(this).val();
$(this).val('');
});
$(this).focusout(function() {
var newvalvalue = $(this).val();
if(newvalvalue == ''){
$(this).val('', valvalue);
}
});
});
focus()
函数工作正常,但是变量 valvalue
没有被 focusout
函数获取。
有人知道将 valvalue
变量传递给第二个 focusout
函数的方法吗?
最佳答案
您需要使 varvalue
对两个事件处理程序可见。可以通过将其声明在他们的范围之外来完成。
$( "#contact input" ).each(function( index ) {
var valvalue; /* both event handlers can see it here */
$(this).focus(function() {
valvalue = $(this).val();
$(this).val('');
});
$(this).focusout(function() {
var newvalvalue = $(this).val();
if(newvalvalue == ''){
$(this).val('', valvalue);
}
});
});
关于javascript - 在两个 Javascript/Jquery 函数之间传递 var?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24226812/