2

誰かがこのコードがどのように機能するかを説明してくれませんか

コードをプラグインファイルに配置するか、ページのヘッドセクションに配置する必要があるかがわからないためです

他に何に注意する必要がありますか

コードはhttp://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/datePickerCloseMouseOut.htmlからのものです

前もって感謝します、リチャード

(また、wat cal が参照していることもわかりません。datePickerDiv と $('.date-pick')?)

 $(function() 
{ 
   var cal; 
   var $this; 

   var checkForMouseout = function(event) 
   { 
      var el = event.target; 

      while (true){ 
         if (el == cal) { 
            return true; 
         } else if (el == document) { 
            $this.dpClose(); 
            return false; 
         } else { 
            el = $(el).parent()[0]; 
         } 
      } 
   }; 

   $('.date-pick') 
      .datePicker() 
      .bind( 
         'dpDisplayed', 
         function(event, datePickerDiv) 
         { 
            cal = datePickerDiv; 
            $this = $(this); 
            $(document).bind( 
               'mouseover', 
               checkForMouseout 
            ); 
         } 
      ).bind( 
         'dpClosed', 
         function(event, selected) 
         { 
            $(document).unbind( 
               'mouseover', 
               checkForMouseout 
            ); 
         } 
      ); 

}); 
4

1 に答える 1

2

このコードは、マウスが datepicker div を離れたかどうかを確認し、マウスが離れた場合は閉じます。コードは、イベントを受け取った要素がカレンダーであったかどうかをチェックすることで、これをチェックします。

//el is set above or below, call is set globally in the document.ready
while (true){ //this will loop forever until a return
     if (el == cal) { //is the receiving element the calender
        return true; //we return true (no ideo why true and not null or 'yaadada'
     } else if (el == document) { //we check if the target el is the document
        $this.dpClose();  //close the element
        return false; //return to leave loop
     } else { //el is neither the call or the document
        el = $(el).parent()[0]; //set el to the imidiate parent of the current el and reloop
     }
} 

このコードをドキュメントの先頭に配置する必要があります。

次のコードの方が簡単です。

$('.date-pick') 
  .datePicker() 
  .bind( 
     'dpDisplayed', 
     function(event, datePickerDiv) 
     { 
        cal = datePickerDiv; //datepickerdiv should somehow hold the the datpicker div , something like: $('.date-pick')[0];
        $this = $(this); 
        $(cal).mouseleave( function() { $(this).dpClose(); });
     }
} 

より良い質問は、「それが何をするかわからないコードを含めたくないのはなぜですか?」ということかもしれません。

注: このコードは非常に醜いので、おそらく書き直すことを検討する必要があります。

于 2009-05-02T08:43:06.900 に答える