【发布时间】:2012-07-27 16:54:11
【问题描述】:
当我执行以下代码时,我希望相同的元素 id 会被提醒两次,但第一个是正确的,而第二个总是显示集合中第一个元素的名称。
$("div.someClass").each(function (index) {
$(this).click(function () {
alert($(this).attr("id")); // Here i get the actually clicked element
$.when($("div.someClass").animate({ }, 0)).then(function () {
alert($(this).attr("id")); // Here i get the first element in of that class
});
});
});
为什么会这样?如何解决?我尝试将元素的名称传递给函数,但没有成功。
【问题讨论】:
-
当您单击一个 div 时,您希望所有具有相同类的 div 都进行动画处理吗?您正在重新选择
$.when中的 div,因为只有在您获取集合中第一个项目的 id 属性时才会调用.then回调。