2011-03-13 24 views
0

お元気ですか、私は以前同様の質問をしています。私は二重投稿のようなことをお詫びします。私はこれを考え出して時間を費やしていて、それまでにはあまり出てこなかった。このシナリオでは可変スコープはどのように機能しますか?

トグルイベントによってトリガされる1つのオーバーアーニング機能と2つの機能があります。そして、私は2番目の関数を値を取得し、2番目の関数に渡すために何かをしたい。

function(){ 

    $('selector').toggle(

     //i want this to gather a value and store it in a variable 
     function(){ }, 

     //and i want this to accept the variable and value from the previous function 
     function(){} 
    )} 

答えて

0

あなたが値を格納および取得するためのセレクタの一致で要素の値を格納するためにjQueryのデータ(http://api.jquery.com/jQuery.data/)を使用することができます。

EXA:奇妙な一部のthats

function(a,b){ 



    $('selector').toggle(

      //i want this to gather a value and store it in a variable 
      function(){ 
      $(this).data("<YOUR_VARIABLE_NAME>", <YOUR_VARIABLE_VALUE>); 
      }, 

      //and i want this to accept the variable and value from the previous function 
      function(){ 
      var someVariable = $(this).data("<YOUR_VARIABLE_NAME>"); 
      } 
     )} 
+0

OK。私は$(this).data()を使用し、値が正しいかどうかを確認するためにalert()を使用しました。最初の関数の正しい値が返されますが、2番目の関数の場合はnullが返されます。すべてが同じでしたが、何とか2番目の関数のためにnullが返されました – Ben

+0

サンプルを投稿しました... plsはそれらの行を試して、まだ問題がある場合はお知らせください... – Chandu

+0

ok私はalert($(this))を使用しました。 data( "variable_name"))を再度呼び出し、最初の関数の正しい値を返しますが、2番目の関数はnullを返します。私はあなたが言ったとおりに正確に...なぜこの仕事をしなかった... – Ben

0
function(){ 
    // this var will be in scope for anything inside this function 
    var availableInBoth; 

    $('selector').toggle(function(){ 
     // this var is only usable in this function 
     var availableHereOnly = 10; 
     availableInBoth = 10; 
    }, function(){ 
     console.log(availableInBoth); 
    }) 
} 
関連する問題