简体   繁体   English

jQuery回调函数传递事件

[英]jQuery callback function pass event

I have a click event as follow which works fine: 我有一个点击事件,如下所示,效果很好:

$('#showmenu').off('click').on('click', function(e) {
    e.preventDefault();
    e.stopPropagation();
    show_menu();
    return false;
});

where show_menu is: show_menu在哪里:

function show_menu(e) {
    if (!collapseForm.is(':visible')) {
        collapseForm.show();
        showMenu.removeClass(chevronDown).addClass(checvronUp);
        searchAgain.hide();
    } else {
        collapseForm.hide();
        showMenu.removeClass(checvronUp).addClass(checvronDown);
        searchAgain.show();
    }
}

I would like to be able to do something like that: 我希望能够做这样的事情:

$('#showmenu').off('click').on('click', show_menu(e));

Is it possible to pass "e" to the callback function by doing the following? 是否可以通过执行以下操作将“ e”传递给回调函数?

function show_menu(e) {
    e.preventDefault();
    e.stopPropagation();
    if (!collapseForm.is(':visible')) {
        collapseForm.show();
        showMenu.removeClass(chevronDown).addClass(checvronUp);
        searchAgain.hide();
    } else {
        collapseForm.hide();
        showMenu.removeClass(checvronUp).addClass(chevronDown);
        searchAgain.show();
    }
    return false;
}

在此处输入图片说明

The event object is passed to the function when the function is called (by the event firing). 调用函数时(通过事件触发)将事件对象传递给函数。

You have to pass the function to the on method. 您必须将函数传递给on方法。

$('#showmenu').off('click').on('click', show_menu);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM