【发布时间】:2011-04-04 00:34:34
【问题描述】:
为什么会出现这个错误?
jQuery(document).ready(function($){
$('.stuff').each(function(){
function thing(id, count){
this.id = id;
this.count = count;
}
var myvar = new Array();
myvar.push(new thing('one', '134'));
myvar.push(new thing('two', '225'));
$('a', this).click(function(event){
var i = $(this).attr('class'); // class is 'one', 'two' etc...
// myvar[i] appears undefined here! wtf?
});
});
});
【问题讨论】:
-
你有一个多余的括号,应该是
var i = $(this).attr('class');还是打错字? -
打错了,错误依旧存在:(
-
@你能在console.log或者alert i value中打印i的值吗,可能是错的...
标签: javascript jquery arrays