我用jQuery的每個循環(我不介意使用for循環,如果代碼工作更好),它的循環通過與類的所有div =」 searchMe「。我想將當前的div存儲在一個變量中,我可以在循環的下一次迭代中使用它來比較一個值和新的當前div。有些代碼被刪除(所有工作)以簡化事情,但這是我的代碼:For循環/每個循環變量的存儲和比較(jQuery的或Javascript
$('.searchMe').each(function(){
var id = $(this);
sortUp += 1,
sortDown -= 1;
if (test) {
id.attr("name", ""+sortUp+"");
}
else {
id.attr("name", ""+sortDown+"");
}
if (id.attr("name") > lastId.attr("name")) {
id.insertBefore(lastId);
}
lastId = id; //this doesn't work, but illustrates what I'm trying to do
});
一切除了最後3行正常工作 這可能與每個/ for循環
'lastId'的作用域是什麼? –