2010-12-10 18 views
0

htmlwireupsを自動化する単一の関数を作成したいと考えています。ページのいくつかの部分はajaxを使ってロードされており、ドキュメントと遅延ロードされたセクションの両方を準備するために単一の関数を呼びたいと思います。jquery準備完了ハンドラへのパラメータの受け渡し

例:

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

単にjQueryのreadyコールでそれを参照せずに、内部機能に至るまでtarget変数を渡します。

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

クールな豆....ちょうどトークンオフ –

+0

私は '$()。ready'が動作するとは確信していません。 '$()'は空のjQueryオブジェクトを返します。そのため、イベントハンドラをバインドすることは効果がありません。 –

+0

'$()。ready(function(){alert(1);});'コールバック関数はいつ実行されますか?すぐに? –

関連する問題