Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用.on函数运行jQuery声明函数_Javascript_Jquery - Fatal编程技术网

Javascript 使用.on函数运行jQuery声明函数

Javascript 使用.on函数运行jQuery声明函数,javascript,jquery,Javascript,Jquery,目前,该功能可以工作: $("#email_address_input").on('focusout', function(){ emailValidationCheck($(this)); }); function emailValidationCheck(e){ ... } 因此,基本上,如果email address输入元素被关注,那么将运行一个匿名函数,该函数调用声明的函数emailValidationCheck(当然,

目前,该功能可以工作:

    $("#email_address_input").on('focusout', function(){
        emailValidationCheck($(this));
    });

    function emailValidationCheck(e){
        ...
    }
因此,基本上,如果email address输入元素被关注,那么将运行一个匿名函数,该函数调用声明的函数
emailValidationCheck
(当然,声明的函数将email address输入元素作为参数)

那个匿名函数感觉是多余的。它所做的只是调用声明的函数,所以在我看来它应该被删除

因此,我试图在事件触发时直接调用声明的函数,而不是调用匿名函数,后者反过来调用声明的函数。如下所示(警告,它无法按预期工作):

问题:我如何才能让它工作?还是原始答案是最佳实践?基本上,我想做的是:当
focusout
事件在指定元素上触发时,我想执行
emailValidationCheck
函数,其中传入的参数就是发生这些事情的元素

谢谢

不需要使用匿名函数作为事件的回调。您可以轻松调用已定义的函数,而无需使用
()
前置函数(因为包含该前置函数将基本上将
emailValidationCheck的返回值传递给回调函数,而不是函数引用本身)。例如:

$("#email_address_input").on('focusout', emailValidationCheck);
function emailValidationCheck(e)
{
    console.log( e );       // logs the event
    console.log( $(this) ); // logs the jQuery object that lost focus
}
现在,您的
emailValidationCheck
函数将在您在函数构造函数中定义的
e
变量中接收事件

由于该函数已绑定为回调函数,
$(this)
在其中也可用。例如:

$("#email_address_input").on('focusout', emailValidationCheck);
function emailValidationCheck(e)
{
    console.log( e );       // logs the event
    console.log( $(this) ); // logs the jQuery object that lost focus
}

您不需要使用匿名函数作为事件的回调。您可以轻松调用已定义的函数,而无需使用
()
前置函数(因为包含该前置函数将基本上将
emailValidationCheck的返回值传递给回调函数,而不是函数引用本身)。例如:

$("#email_address_input").on('focusout', emailValidationCheck);
function emailValidationCheck(e)
{
    console.log( e );       // logs the event
    console.log( $(this) ); // logs the jQuery object that lost focus
}
现在,您的
emailValidationCheck
函数将在您在函数构造函数中定义的
e
变量中接收事件

由于该函数已绑定为回调函数,
$(this)
在其中也可用。例如:

$("#email_address_input").on('focusout', emailValidationCheck);
function emailValidationCheck(e)
{
    console.log( e );       // logs the event
    console.log( $(this) ); // logs the jQuery object that lost focus
}

javascript不是这样工作的。.on()函数需要一个函数作为参数。您可以传递匿名函数或函数名。一旦将()放在末尾,它就会内联执行函数并将结果传递给.on()函数。

javascript不是这样工作的。.on()函数需要一个函数作为参数。您可以传递匿名函数或函数名。一旦您将()放在末尾,它就会内联执行函数并将结果传递给.on()函数。

您可以粘贴从
emailValidationCheck
函数到
.on()中的匿名函数的所有内容
您可以粘贴所有内容,从
emailValidationCheck
函数到
中的匿名函数。on()
emailValidationCheck函数的参数如何?当我调用它时,它不是必须指定吗?当我在emailValdiationCheck函数中时,如何引用该参数?@Neil不,你没有。jQuery自动将事件传递给回调函数,并将
这个
变量传递给它,因此它在回调的范围内。你发表评论时,我正在编辑答案。请查看更新。emailValidationCheck函数的参数是什么?当我调用它时,它不是必须指定吗?当我在emailValdiationCheck函数中时,如何引用该参数?@Neil不,你没有。jQuery自动将事件传递给回调函数,并将
这个
变量传递给它,因此它在回调的范围内。你发表评论时,我正在编辑答案。请查看更新。