I have an ajax script that loads list items on my page. Depending what tab the user clicks on, different lists will open. The list items are unique. But my if clause figures out what list I'm in then calculates the offset for a popup window.
if(varType == 'search')
{
p = $('.dv_variable_' + subjectId + '_' + varNumber + '_' + varSequence + '_search').offse开发者_如何学JAVAt();
//p.top = $('.dv_variable_' + subjectId + '_' + varNumber + '_' + varSequence + '_search').offset().top - $(window).scrollTop();
//p.left = $('.dv_variable_' + subjectId + '_' + varNumber + '_' + varSequence + '_search').offset().left();
}
else if(varType == 'fuv')
{
p = $('.dv_variable_' + subjectId + '_' + varNumber + '_' + varSequence + '_fuv').offset();
}
And then I do this if the offset is 0, 0:
while(p.top == 0 || p.left == 0)
{
if(varType == 'search')
{
p = $('.dv_variable_' + subjectId + '_' + varNumber + '_' + varSequence + '_search').offset();
}
else if(varType == 'fuv')
{
p = $('.dv_variable_' + subjectId + '_' + varNumber + '_' + varSequence + '_fuv').offset();
}
}
Now if I do alert(p.left+" "+p.top) between the ifs above and the while loop, the offset is calculated, otherwise without the alert I always get 0,0.
This is likely a heisenbug, as also posted here: Why would jquery return 0 for an offsetHeight when firebug says it's 34?
The DOM likely hasn't had time to update after you set it or do some showing/hiding (perhaps not shown?). The alert solves this issue by introducing a small delay, enough for the DOM to update.
精彩评论