是否准备好jQuery ready事件?
问题描述:
我在页面上使用jQuery $(document).ready()
事件。一切都很好,除了
如果我使用Ajax调用加载数据, $(document).ready()
事件不会触发。我猜它的行为是因为页面已经加载了,我只是将更多的数据从Ajax响应中添加到DOM中。
I am using the jQuery $(document).ready()
event on page. All is fine, except that
if I am loading data using Ajax calls, the $(document).ready()
event does not fire. I guess that it behave in such way because the page was already loaded and I am just adding more data from the Ajax response to the DOM.
我怎样才能重新启动 ready event?
How can i refire the ready
event ?
答
如果你需要执行一些额外的Javascript,你可能会使用你调用Ajax回调onComplete事件的函数:
If you need to execute some additionnal Javascript, you might use a function that you call upon Ajax callback onComplete event :
function initJS(){
//your code here
}
$.ajax({
url: 'ajax/test.html',
success: function(data){
},
complete: function(){
initJS();
}
});