简体   繁体   English

函数不能用作另一个函数参数

[英]function doesn't work as another function parameter

Failed Example 失败的例子

Original Example 原始例子

Can anyone tell me how to pass the draggable function as a parameter for clickit , which is an AJAX function? 谁能告诉我如何将draggable函数作为clickit的参数clickitclickit是AJAX函数? I want to pass draggable as a parameter to attach it to a dynamically added element. 我想将draggable作为参数传递,以将其附加到动态添加的元素上。 I can't get it to work defining the draggable function and pass it to clickit . 我无法定义可拖动函数并将其传递给clickit

function clickit(fun){
  $.ajax({
        'url' : "url",
        'dataType' : 'json',
        'success' : function(data){
         var item_html ="";
            $.each(data.query.results.json,function(i,k){
                item_html += '<div class="dialog"><h3>'+k+'</h3></div>'
            });           
            $('.area').html(item_html);
            fun;         
          }
   });   
}
$('button').click(function(){
    var funpara = $('.dialog').draggable();
    clickit(funpara)
});

Here's the one that works: 这是可行的一种:

function clickit(){
  $.ajax({
        'url' : "url",
        'dataType' : 'json',
        'success' : function(data){
         var item_html ="";
            $.each(data.query.results.json,function(i,k){
                item_html += '<div class="dialog"><h3>'+k+'</h3></div>'
            });           
            $('.area').html(item_html);
            $('.dialog').draggable();         
          }
   });   
}

$('button').click(function(){
    clickit()
});

It looks like the parameter fun cannot be passed to the success function. 看来参数fun无法传递给成功函数。

It looks like you want to invoke fun , but currently you're just referencing to it without the invocation () 看起来您想调用fun ,但是当前您只是在没有调用()情况下引用它

function (data) {
    var item_html = "";
    $.each(data.query.results.json, function (i, k) {
        item_html += '<div class="dialog"><h3>' + k + '</h3></div>';
    });           
    $('.area').html(item_html);
    fun();         
}

In your context, it looks like you want to invoke $('.dialog').draggable() , so fun should look more like this 在您的上下文中,您似乎想调用$('.dialog').draggable() ,所以fun应该更像这样

function fun(o) {
    return $('.dialog').draggable(o);
}

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

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