Jquery 动态创建表格时滚动到表格行

Jquery 动态创建表格时滚动到表格行,jquery,Jquery,我有一张桌子,我正在里面填 $(document).ready(function () { PopulateMyTable(); // here scroll to the right row // 1015 is a dynamic generated id of row. $('html, body').animate({ scrollTop: $("tr[data-id=" + 1015 + "]").offset().top

我有一张桌子,我正在里面填

$(document).ready(function () { 
     PopulateMyTable(); 
     // here scroll to the right row
     // 1015 is a dynamic generated id of row.

     $('html, body').animate({
        scrollTop: $("tr[data-id=" + 1015 + "]").offset().top
    }, 2000);       
});
现在的问题是,当我使用调试器时,即使代码通过
PopulateMyTable
方法传递,但它并没有用HTML填充表,就像我在开发人员工具中查找一样,结果我得到了这个错误

例外情况

无法在未定义上使用top函数

我正在动态添加行

// get data from rest API and  it

function PopulateMyTable() 
{
       $("#MyTable").append("<tr data-id='" + ID +"' onclick='DoSomething(" + ID + ")'>"); 
}
//从rest API和it获取数据
函数PopulateMyTable()
{
$(“#MyTable”)。追加(“”);
}

PopulateMyTable()
中的
ID
是什么?它是在哪里定义的?@P5Coder请阅读注释,它来自rest Api:-)当您调用$(“tr[data id=“+1015+”])时,看起来行还不在DOM中