简体   繁体   English

在jQuery中创建和调用函数

[英]creating and calling functions in jquery

I'm new with jquery and I'm having problems in constructing functions. 我是jquery新手,在构造函数时遇到问题。 I need some help on this one? 我需要这方面的帮助吗? I'm not sure I've done the correct thing with this functions? 我不确定我是否已使用此功能完成了正确的操作?

Please explain what is rong in how I wrote the next functions or tell me how to build the correct syntax The function member.panel.init('login') doesn't do the right thing. 请说明我如何编写下一个函数,或告诉我如何构建正确的语法是什么错误函数member.panel.init('login')不能正确执行操作。

member = {};
member.panel = function(){
    return{
    init: function(a)
    {   
        $('#log_in .login').click(open_menu(a));
        $('#log_in .register').click(open_menu(a));
    },

    open_menu: function(what)
    {
         if(what!='login' || what!='register') what='login';
         $('#q_login_dialog #menu-'+what+'').addClass("q_dialog_panel_item_active");
         if(what=='login')
         {
            $('.q_dialog_content #dialog-form3').css("display", "none");
            $('.q_dialog_content #dialog-form2').css("padding-bottom", "20px");
            $(".login-box").fadeIn('fast'); 
         }
         if(what=='register')
         {
            $('.q_dialog_content #dialog-form2').css('display', '');
            $('.q_dialog_content #dialog-form3').css("padding-bottom", "20px");
            $(".login-box").fadeIn('fast'); 
         }   
    }
}
}();
$(function () {
   member.panel.init('login');
});

When you do this: 执行此操作时:

open_menu(a)

...you're calling that function. ...您正在调用该函数。 For a handler, you want to pass a function to click() . 对于处理程序,您想一个函数传递click() The way you're doing it, it looks like you want to retain the value that is passed to init . 这样做的方式似乎要保留传递给init的值。 This would mean that you need to have your open_menu function return a function that references that value. 这意味着您需要使open_menu函数返回一个引用该值的函数。

Unfortunately, you don't show a function named open_menu that is accessible that way. 不幸的是,您没有显示可通过这种方式访问​​的名为open_menu的函数。 Your open_menu function is the member of an object, so that's how you need to access it. 您的open_menu函数是对象的成员,因此您需要访问它。

Your code should look like this: 您的代码应如下所示:

member = {};
member.panel = function () {
    return {
        init: function (a) { // use "this.open_menu" to refer to the function
            $('#log_in .' + a).click(this.open_menu(a));
        },
        open_menu: function (what) {

            // "open_menu" now returns a handler function that references "login"
            return function () {
                if (what != 'login' || what != 'register') what = 'login';
                $('#q_login_dialog #menu-' + what + '').addClass("q_dialog_panel_item_active");
                if (what == 'login') {
                    $('.q_dialog_content #dialog-form3').css("display", "none");
                    $('.q_dialog_content #dialog-form2').css("padding-bottom", "20px");
                    $(".login-box").fadeIn('fast');
                }
                if (what == 'register') {
                    $('.q_dialog_content #dialog-form2').css('display', '');
                    $('.q_dialog_content #dialog-form3').css("padding-bottom", "20px");
                    $(".login-box").fadeIn('fast');
                }
            };
        }
    }
}();
$(function () {
    member.panel.init('login');
    member.panel.init('register');
});

Or here's an alternative that is closer to Rob W's comment, but fixes the open_menu function reference. 或者这是一种更接近Rob W的注释的替代方法,但它修复了open_menu函数引用。

member = {};
member.panel = function () {
    return {
        init: function (a) {
            var self = this;
            $('#log_in .' + a).click(function() {
                self.open_menu(a); 
            });
        },

        open_menu: function (what) {
            if (what != 'login' || what != 'register') what = 'login';
            $('#q_login_dialog #menu-' + what + '').addClass("q_dialog_panel_item_active");
            if (what == 'login') {
                $('.q_dialog_content #dialog-form3').css("display", "none");
                $('.q_dialog_content #dialog-form2').css("padding-bottom", "20px");
                $(".login-box").fadeIn('fast');
            }
            if (what == 'register') {
                $('.q_dialog_content #dialog-form2').css('display', '');
                $('.q_dialog_content #dialog-form3').css("padding-bottom", "20px");
                $(".login-box").fadeIn('fast');
            }
        }
    }
}();
$(function () {
    member.panel.init('login');
    member.panel.init('register');
});

Rob W is right Rob W是正确的

Just for explanation: The first parameter for .click() is supposed to be a function reference. 只是为了说明:.click()的第一个参数应该是函数引用。 In your code the first parameter is the result of the immediate call(!) to the function open_menu() with parameter a. 在您的代码中,第一个参数是带有参数a的函数open_menu()的立即调用(!)的结果。 Only if the result (the return value) of this call were a function reference this would result in a valid .click() parameter. 仅当此调用的结果(返回值)是函数引用时,才会产生有效的.click()参数。

If you move your code into an anonymous function() like Rob W proposed, it will result in a function reference (to this anonymous function) which will only be executed when the click event is fired. 如果将代码移至Rob W提议的匿名函数()中,它将导致一个函数引用(对此匿名函数的引用),该引用仅在触发click事件时执行。

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

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