gpt4 book ai didi

javascript - jQuery - 从处理函数接收正确的变量值

转载 作者:行者123 更新时间:2023-11-30 12:54:54 25 4
gpt4 key购买 nike

<分区>

我在编写 jQuery 插件时遇到了一个小问题——无法从事件的处理程序函数中获取变量。看看我的例子来理解:

(function( $ ){

var methods = {

init : function( options ) {

var settings = $.extend( {
'images': [['1.jpg'],['2.jpg'],['3.jpg']]
}, options);

var lastim=2; //just for test

$.each(settings.images,function(event) {

console.log(lastim); //Getting 2, Ok!

img=new Image();
img.src=settings.thumbPath+'/'+this[0];

$(img).load(function(event)
{
lastim=5;
});
});

console.log(lastim); //Getting 2, expecting 5

}};

$.fn.testSlider = function( method ) {
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'No such method'+method );
}
};

})( jQuery );

如何在每个函数后得到 5 in lastim 变量?预先感谢您的帮助!

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com