When the user clicks a link, I need to scroll up to the showemaildiv
and then fire a click event to open the email data. This works:
$(".showemaillink").click(function(e) {
e.preventDefault();
var emailid = $(this).attr('data-emailid');
var emaildiv = $(".showemaildiv[data-id='" + emailid + "']");
$('html, body').animate({
scrollTop: emaildiv.offset().top
}, 500);
emaildiv.click();
});
But to make it a little nicer, I'd like the email to expand after the animate so I'm using a callback. In this case, however, emaildiv.click()
is not working although the alert
is - what is wrong here?
$(".showemaillink").click(function(e) {
e.preventDefault();
var emailid = $(this).attr('data-emailid');
var emaildiv = $(".showemaildiv[data-id='" + emailid + "']");
$('html, body').animate({
scrollTop: emaildiv.offset().top
}, 500, function(){
emaildiv.click();
alert('callback fired');
}
);
});
答案 0 :(得分:0)
你的代码还可以正常工作(我已经尝试过了)。 但是回调函数可能存在问题 - 对于你动画的每个元素,它都被调用两次 - 第一次用于html,第二次用于body。
您可以在此处详细了解:Callback of .animate() gets called twice jquery
如果您在emaildiv
点击事件中有一些操作否定了之前的操作(例如切换),则表明它不起作用。