Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 不能正确使用闭包_Javascript_Jquery - Fatal编程技术网

Javascript 不能正确使用闭包

Javascript 不能正确使用闭包,javascript,jquery,Javascript,Jquery,我编写了JS函数,它必须根据数组中的值绑定它生成的按钮。 但它给了我最后一个值。我读到我必须使用闭包,我做到了,但我仍然无法正确绑定它们! 我还是个初学者 我读到关于结束的文章,我有了这个想法,但仍然不知道我错过了什么 function addNewServices(newServicesArray){ var j=0; var x; for (i in newServicesArray){ var html=''; html='<div

我编写了JS函数,它必须根据数组中的值绑定它生成的按钮。 但它给了我最后一个值。我读到我必须使用闭包,我做到了,但我仍然无法正确绑定它们! 我还是个初学者 我读到关于结束的文章,我有了这个想法,但仍然不知道我错过了什么

function addNewServices(newServicesArray){
    var j=0; var x;
    for (i in newServicesArray){
        var html='';

        html='<div style="width: 33%; float: leftt"><a href="#" data-role="button" data-icon="home" id="btn-'+newServicesArray[j].servicename+'" value="'+newServicesArray[j].servicename+'" class="ui-btn-up-c">'+newServicesArray[j].servicename+'</a></div>';
        $("#main-menu").append(html);


        $('#btn-'+newServicesArray[j].servicename).bind('click', function (){bindThis(j)});
        j++;
    }

    var bindThis = function( j ) {
        return function() {
            alert(j); // gives 2 always
            alert( newServicesArray[j].servicename ); 
        };
    };
}
函数addNewServices(newServicesArray){
var j=0;var x;
for(newServicesArray中的i){
var html='';
html='';
$(“#主菜单”).append(html);
$('#btn-'+newServicesArray[j].servicename).bind('click',function(){bindThis(j)});
j++;
}
var bindThis=函数(j){
返回函数(){
警惕(j);//总是给出2
警报(newServicesArray[j].servicename);
};
};
}
因为你有

function (){bindThis(j)}
当j的值为2时,会调用它

你只需要

bindThis(j)

使用不同的值调用它,您不必绑定循环中的单击。。。您可以通过函数中的
$(this)
获得单击的引用

尽我所能让事情变得简单

function addNewServices(newServicesArray){
   var j=0; 
   for (i in newServicesArray){
      var html='';

      html='<div style="width: 33%; float: left"><a href="#" data-role="button" data-icon="home" id="btn-'+newServicesArray[j].servicename+'" value="'+newServicesArray[j].servicename+'" class="ui-btn-up-c">'+newServicesArray[j].servicename+'</a></div>';

      $("#main-menu").append(html);


   }
}

$(function(){
  $(document).on('click','a[id^="btn-"]',function (){
      var $this = $(this);
      alert($this.attr('value')); 
  });
});
函数addNewServices(newServicesArray){
var j=0;
for(newServicesArray中的i){
var html='';
html='';
$(“#主菜单”).append(html);
}
}
$(函数(){
$(文档)。在('click','a[id^=“btn-”])上,函数(){
var$this=$(this);
警报($this.attr('value'));
});
});

闭包只是函数从外部作用域访问变量的方式。这里的关键词是variable-variable可能会更改,如果您稍后(单击)访问它,您将访问它的更高版本

因此,无论如何,您需要存储
j
j
th按钮的关联。多亏了jQuery,
bind
方法已经有了这样一个功能:它的
eventData
是一些将传递给事件处理函数的用户数据

因此,改变这一点:

(..).bind('click',function (){bindThis(j)});
为此:

(..).bind('click', j, bindThis);
…应该工作。注意,我们不需要创建任何包装函数。我们只需将
bindThis
函数本身传递给
bind
,并告诉
bind
它将在调用它时传递
j


(*)-尚未测试

使用jQuery的
$。每次
迭代都会更容易…您不应该在循环中声明
bindThis
。为什么你声明了x却从不使用它?@fragmentedreality实际上,bindThis不是在循环中声明的。