Callback after ajax loading a tab

不羁的心 提交于 2019-12-01 03:48:35

问题


How can I apply some code to the content of an ajax loaded tab? I tried using $(document).ready inside the loaded content, but that prevented css styles from loading (don't know why).

Is there a callback function? Should I use $(document).ready and styles inside the loaded document in some other way?

If using $(document).ready inside the loaded document is fine, should I also include references to jquery and its plugins in it?


回答1:


Have you tried the load event? This should be called, when the contents of the tab have been loaded.

In general you shouldn't treat the loaded element as a new page and call the $(document).ready. This is not a new page, but some new elements added to the DOM. All ajax methods feature a callback method that is invoked, when the data are successfully loaded.




回答2:


What code are you using to load the content through ajax? If you using a jQuery command like load or ajax then I would recommend putting your code inside the callback function. For example, using load

$('#myUITab').load('anotherPage.html', function() {

    // put the code you need to run when the load completes in here

});



回答3:


jQuery UI "Tabs" provide callback method. Check out below!

$( "#tabs" ).tabs({
    ajaxOptions: {
        error: function( xhr, status, index, anchor ) {
            $( anchor.hash ).html(
                "error occured while ajax loading.");
        },
        success: function( xhr, status ) {
            //alert("ajax success. ");    //your code
        }
    }
});



回答4:


Another way to do this is by using ajaxComplete:

$("#loading_comtainer").ajaxComplete(function( event,request, settings ){
   alert("ajaxCompleted");
});



回答5:


You can use the tabsload event http://jqueryui.com/demos/tabs/#event-load

See how it can work in the example below:

$('#nav_tabs').tabs({
    select: function(event, ui){
        var url = $.data(ui.tab, 'load.tabs');
        if (url == '?ak=/account/logout') { 
            //exclude the logout from using ajax
            location.href = url;
            return false;
        }
        return true;
    },
}).bind('tabsload',function(event, ui){
    alert('The tab is loaded. What now?');
});


来源:https://stackoverflow.com/questions/740652/callback-after-ajax-loading-a-tab

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