代码之家  ›  专栏  ›  技术社区  ›  Mircea

jQuery调用预定义函数

  •  1
  • Mircea  · 技术社区  · 16 年前

    $('#element').click(function(big){
        some big function
        ...
        ...
    });
    
    
    $('#another_element').click(function(){
        this should execute the previous "big" function
    });
    

    桑克斯。

    2 回复  |  直到 16 年前
        1
  •  6
  •   tadman    16 年前

    如果您在调用外部定义函数,您可以随意多次使用它。

    var bigfunction = function(big) { ... };
    
    $('#element').click(bigfunction);
    $('#another_element').click(bigfunction);
    

    您也可以按常规方式声明:

    function bigfunction(big) { ... };
    
        2
  •  3
  •   Ju Nogueira    16 年前

    function the_big_function(){
       // do stuff
    }
    
    $('#element').click(function(big){
       the_big_function();
    });
    
    
    $('#another_element').click(function(){
        the_big_function();
    });
    
    推荐文章