Refire the jQuery ready event?

☆樱花仙子☆ 提交于 2020-01-03 03:06:13

问题


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.

How can i refire the ready event ?


回答1:


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();
  }
});



回答2:


.load(), .bind(), or .live() will be your friends here....




回答3:


just break the logic that is in the "$(document).ready( function() {});" block out into a separate function. Then the page will call it once when it is "ready", and you can directly call that function when you want to do a refresh.




回答4:


I think the person asking is because most developer doing jQuery plugins i.e wordpress plugins use the "ready" function.

Everyone here is saying go modify the plugins !

I'm facing the same problem as I'm developing AJAX template for wp and most plugins don't work !



来源:https://stackoverflow.com/questions/3485073/refire-the-jquery-ready-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!