2010-12-10 91 views
0

我想創建一個函數來自動執行htmlwireups。頁面的某些部分使用ajax加載,我想調用一個函數來準備文檔和延遲加載的部分。將參數傳遞給jquery ready handler

實施例:

function WireHtml(target){ 
    $(target).ready(function(target){ 

     // call anything you would normally wire in a standard ready 
     // but only on the descendants of the target node such as wiring up 
     // an accordion 
     $(target).find(".accordion").accordion(); 

    } 
} 

回答

2

只需通過對內部函數傳遞target變量而不在jQuery的ready呼叫引用它。

function WireHtml(target){ 
    $(function(){ // <- note the lack of "target" references 

     // call anything you would normally wire in a standard ready 
     // but only on the descendants of the target node such as wiring up 
     // an accordion 
     $(target).find(".accordion").accordion(); 

    }); 
} 

target變量將是附加到準備處理因closure在函數中使用。

備註:$(document).ready(yourFunction)$(yourFunction)優於$().ready(yourFunction),儘管它們都是等效的。

+0

很酷的豆....只是一個令牌關 – 2010-12-10 21:37:44

+0

我不相信'$()。ready'工作。 '$()'會返回一個空的jQuery對象,所以綁定事件處理程序應該不起作用。 – 2010-12-10 21:38:06

+0

'$()。ready(function(){alert(1);});'回調函數何時執行?立即? – 2010-12-10 22:03:44