Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.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-如何在作为参数传递的函数中使用时不覆盖var?_Javascript_Jquery - Fatal编程技术网

Javascript-如何在作为参数传递的函数中使用时不覆盖var?

Javascript-如何在作为参数传递的函数中使用时不覆盖var?,javascript,jquery,Javascript,Jquery,我得到了一个for循环,里面有一个名为path的变量,我在一个函数中使用这个变量,作为另一个函数的参数传递,如下所示: for( ... ){ var path = ...; $("<button>").html(path).click(function(){ ...want to use path here... }).appendTo(...); } (…)的{ var路径=。。。; $(“”).html(路径)。单击(函数(){…要在此处使用路径…})。附加到(…);

我得到了一个for循环,里面有一个名为path的变量,我在一个函数中使用这个变量,作为另一个函数的参数传递,如下所示:

for( ... ){
  var path = ...;
  $("<button>").html(path).click(function(){ ...want to use path here... }).appendTo(...);
}
(…)的
{
var路径=。。。;
$(“”).html(路径)。单击(函数(){…要在此处使用路径…})。附加到(…);
}
问题是,在for循环的每个新回合中,路径都会被覆盖,我的完整函数如下所示:

this.goTo = function(path){
    that.path = path;
    that.files = [];
    that.selectedFiles = [];

    //Display path
    that.pathDiv.empty();
    var dirs = that.path.split("/");
    if(dirs.length > 1 && dirs[dirs.length-1].length == 0){
        dirs.splice(dirs.length-1, 1);
    }
    for(var d in dirs){
        var path = "/";
        for(var i=0;i<d;i++){
            if(dirs[i].length > 0){
                path += dirs[i] + "/";
            }
        }
        var name = "/";
        if(dirs[d].length > 0){
            path += dirs[d] + "/";
            name = dirs[d];
        }
        that.pathDiv.append($("<button>").html(name).click(function(){ console.log(path); that.goTo(path); }));
    }

    that.directoryContent.empty();
    serverUI.emit("getDirectoryContent", {path: that.path});
}
this.goTo=函数(路径){
路径=路径;
that.files=[];
that.selectedFiles=[];
//显示路径
that.pathDiv.empty();
var dirs=that.path.split(“/”);
如果(dirs.length>1&&dirs[dirs.length-1].length==0){
直接拼接(直接长度-1,1);
}
用于(变量d,单位为dirs){
var path=“/”;
对于(变量i=0;i 0){
路径+=dirs[i]+“/”;
}
}
var name=“/”;
if(dirs[d]。长度>0){
路径+=dirs[d]+“/”;
name=dirs[d];
}
that.pathDiv.append($(“”).html(名称)。单击(函数(){console.log(路径);that.goTo(路径);});
}
.directoryContent.empty();
emit(“getDirectoryContent”,{path:that.path});
}

我不能在html标记中使用onClick属性,因为我需要访问触发函数的对象(即),知道吗?

您可以将路径作为属性附加到元素本身,如下所示

for( ... ){
  var path = ...;
  $("<button>").attr("path",path).html(path).click(function(){  
     $(this).attr("path")  // use it like this.
 ...want to use path here... }).appendTo(...);
}
(…)的
{
var路径=。。。;
$(“”).attr(“路径”,path).html(路径)。单击(函数(){
$(this.attr(“path”)//像这样使用它。
…要在此处使用路径…});
}

您可以将路径作为属性附加到元素本身,如下所示

for( ... ){
  var path = ...;
  $("<button>").attr("path",path).html(path).click(function(){  
     $(this).attr("path")  // use it like this.
 ...want to use path here... }).appendTo(...);
}
(…)的
{
var路径=。。。;
$(“”).attr(“路径”,path).html(路径)。单击(函数(){
$(this.attr(“path”)//像这样使用它。
…要在此处使用路径…});
}