send a jquery callback function inside a variable

后端 未结 5 623
小鲜肉
小鲜肉 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:18

    Since you are passing a text string you would need to eval it:

    function example(file, targetwidget, callback){
    
        $(targetwidget).load(file, {limit: 25}, function(){
            eval(callback);
        });
    

    Edit:

    if you want to use call() you would have to do it like this:

    function callback() {
        $("#widget_accordion").accordion({fillSpace: true});
    }
    
    function example(file, targetwidget, callback){
    
        $(targetwidget).load(file, {limit: 25}, function(){
            if(typeof(callback)==='function'){
                callback.call(this);
            }
        });
    
    example('http://example.com/', "#divid", callback);
    

    In order for call to work, you would have to pass a function and not a string. But by wrapping your jQuery expression inside of a function, it would be run when the function is called. Calling a function can be done with the .call() method.

提交回复
热议问题