2

クリックイベントを以下の関数にバインドしようとしていますが、ドキュメントの準備ができてバインドされているときに、関数全体が現在実行されています。

クリックイベントだけで実行することはできますか?おそらくそれは私のメソッドの構成方法と関係がありますか?

前もって感謝します

    $(function() {
        $("#expand-search").on("click", search.resize());
    });

    var search = {

        element: $('#search_advanced'),

        resize: function() {
            search.element.slideToggle(400, 'swing', search.buttonState());
        },
        buttonState: function() {
            if(search.element.is(':hidden')) {
                console.log('hidden');
            } else {
                console.log('visible');
            }
        }
    };
4

2 に答える 2

3

関数(handler)の(name)をon()functionに渡す代わりに、(handler)を呼び出しています。reference

変化する

 $("#expand-search").on("click", search.resize());

 $("#expand-search").on("click", search.resize);
于 2013-02-18T09:15:04.817 に答える
1

No parenthesis to event handlers! You want to pass the function-to-be-executed, not the result from executing it. Also, you will need to move your search object inside the ready handler since you use selectors for its initialisation.

$(function() {
    var search = {
        element: $('#search_advanced'),
        resize: function() {
            search.element.slideToggle(400, 'swing', search.buttonState);
        },
        buttonState: function() {
            if(search.element.is(':hidden')) {
                console.log('hidden');
            } else {
                console.log('visible');
            }
        }
    };
    $("#expand-search").on("click", search.resize);
});
于 2013-02-18T09:19:21.317 に答える