2013-03-12 59 views
0

我想申請一個jQuery tabs做一個動態加載的內容。問題是在加載容器之前應用了tabs函數,因此我看不到渲染的選項卡。調試我不能等待一段時間,它可以成功運行。如何等待一個要加載的元素才能應用JQuery功能

我的背景是一個主/細節網格,將細節顯示爲可展開的行。每次點擊一個網格行時,我都會通過ajax詢問詳細信息到另一個名爲mypage.aspx的頁面,並將收到的數據放到一個名爲contentDetaildiv,這實際上是一個動態生成的tr。一旦獲得了數據,我想將tabs函數應用於mypage.aspx內容。

這裏是我的代碼:

var html = '<tr id="newRow"><td colspan="4"><div id="contentDetail"></div></td></tr>'; 

$('#mygrid tr').each(function() { 
    $(this).on('click', function() { 
     // I create a row on the fly and append it next to the clicked row 
     // ... 
     // I load the contents 
     $('#contentDetail').load('mypage.aspx #div1', { data: '123' }, function (response, status, xhr) { 
      if (status == "error") { 
       // ERROR 
      } 
     }); 
     // Here I want to apply the tabs 
     $('#contentDetail').ready(function() { 
      $('.tabs').tabs(); 
     }); 

    }); 

更新:

$.when($('#contentDetail').load('mypage.aspx #div1', { data: '123' }, function (response, status, xhr) { 
      if (status == "error") { 
       // ERROR 
      } 
     })).then($.getScript('Scripts/function.js')); 

function.js$('#tabs').tabs();

回答

0

我解決它使用else子句load回調函數:

if (status == "error") { 
    // Error 
} 
else { 
    // HERE THE CONTENT IS FULLY LOADED 
    $('#tabs').tabs(); 
} 
0

可以使用jQuery when/Deferred做到這一點:

$.when($('#contentDetail').load('mypage.aspx #div1')).then($('.tabs').tabs()); 
+0

不工作謝謝。 – anmarti 2013-03-12 13:46:26

1

試試這個:

var html = '<tr id="newRow"><td colspan="4"><div id="contentDetail"></div></td></tr>'; 

    $('#mygrid tr').each(function() { 
    $(this).on('click', function() { 
     // I create a row on the fly and append it next to the clicked row 
     // ... 
     // I load the contents 
     $('#contentDetail').load('mypage.aspx #div1', { data: '123' }, function (response, status, xhr) { 
      if (status == "error") { 
       // ERROR 
      } 
      if(status == "success"){ 
       // Here I want to apply the tabs 

      $('.tabs').tabs(); 

       } 

     }); 


    }); 
}); 
+0

這是有用的,但它幾乎與我的一樣。 – anmarti 2013-03-12 15:44:35

+0

@Arasa幾乎只有成功而已 – Peeyush 2013-03-12 15:56:24

相關問題