send a jquery callback function inside a variable

后端 未结 5 624
小鲜肉
小鲜肉 2021-02-08 17:52

I have this jquery function

    function example(file, targetwidget, callback){

    $(targetwidget).load(file, {limit: 25}, function(){
        $(\"#widget_acco         


        
5条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-08 18:10

    To pass the callback around, the variable needs to be of type function. Any of these should work:

    function example(file, targetwidget, callback) {
      $(targetwidget).load(file, {limit:25}, callback);
    }
    
    // Call it by providing the function parameter via inline anonymous function:
    example('http://example.com/', "#divid", function() {
      $("#widget_accordion").accordion({fillSpace: true});
    });
    
    // Or, declare a function variable and pass that in:
    var widgetCallback = function() {
      $("#widget_accordion").accordion({fillSpace: true});
    };
    
    example('http://example.com/', "#divid", widgetCallback);
    
    // Or, declare the function normally and pass that in:
    function widgetCallback() {
      $("#widget_accordion").accordion({fillSpace: true});
    }
    
    example('http://example.com/', "#divid", widgetCallback);
    

提交回复
热议问题