Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.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_Arrays - Fatal编程技术网

Javascript 从函数变量中检索数组

Javascript 从函数变量中检索数组,javascript,jquery,arrays,Javascript,Jquery,Arrays,如何从以前的函数中检索此 function bla(my_array) { image_tag[this] = $('<img src="'+ random_url +'"/>'); image_tag[this].appendTo('body'); image_tag[this].bind('load', function () { alert('this'); //instead of alerting "i

如何从以前的函数中检索此

function bla(my_array) {
        image_tag[this] = $('<img src="'+ random_url +'"/>');
        image_tag[this].appendTo('body');
        image_tag[this].bind('load', function () {
            alert('this'); //instead of alerting "image_tag[4]", I'd like to alert its array "4"   
        });
    });
}

bla(4);
函数bla(my_数组){
图像标签[this]=$('');
image_标记[this].appendTo('body');
image_标记[this]。绑定('load',函数(){
alert('this');//我不想提醒“image_tag[4]”,我想提醒它的数组“4”
});
});
}
bla(4);

使用$.proxy将以前的“this”绑定到新函数:

image_tag[this].load($.proxy(function() {
    alert(this);
},this));

我认为您的示例代码的工作方式与您认为的不同:

但假设在实际代码中,一切正常,您只想从click处理程序内部的外部函数中引用
,则可以执行以下操作:

function bla(my_array) {
    var that = this;
    image_tag[this].bind('load', function () {
        alert(that); //instead of alerting "image_tag[4], I'd like to alert 4   
    });
}

我们不知道数组是什么,这个是什么,图像标签是什么,我的数组是什么,或者你想做什么?为什么你要把它设置为4?我很抱歉,我没有包括整个代码,因为我认为它会让事情更加混乱。在你的示例代码中,
将引用
窗口
,而
我的数组
的值将是
4
。我的问题现在清楚了吗?这就行了!我看到您已经更改了绑定。('load'改为just
load
,请问区别是什么?.load只是.bind(“load”)的缩写--jQuery有很多(如.click、.keyup等)。