gpt4 book ai didi

javascript - 在两个 Javascript/Jquery 函数之间传递 var?

转载 作者:行者123 更新时间:2023-11-29 16:12:34 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com