【问题标题】:event mouseout on datepicker日期选择器上的事件 mouseout
【发布时间】:2010-10-23 07:10:33
【问题描述】:

有人能解释一下这段代码是如何工作的吗

因为,我不知道我是否应该将代码放在插件文件中 或从页面上删除标题部分

还有什么需要注意的

代码来自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 
            ); 
         } 
      ); 

}); 

【问题讨论】:

    标签: jquery plugins datepicker bind mouseout


    【解决方案1】:

    此代码将检查鼠标是否离开 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(); });
         }
    } 
    

    一个更好的问题可能是,你为什么不包含你不知道它做什么的代码?

    注意:这段代码非常难看,你应该考虑重写它。

    【讨论】:

    • 谢谢,你说对了,我也是 jquery 新手,所以我不完全理解语法如果你说它难看,我也只能相信你的话。但我真正想要的是,也有 datepicker 的演示页面中提供的功能,但它没有提供有关如何调整它使其工作的信息。 kelvinluck.com/assets/jquery/datePicker/v2/demo/…我有点迷路了。 Y 在用于创建弹出日历的插件文件中找到了一个 div id,但我无法使其工作
    • 我希望有人可以看一下整个脚本,因为我无法弄清楚。因此,与此同时,我放弃了可以让它发挥作用的想法。我也给创作者发了电子邮件,看看我能不能得到他的回应。如果您看到演示页面源代码,您将看到它使用完全相同的脚本而无需修改,并且由于某种原因它可以在该页面上运行。因此,我的头脑过于清醒似乎毫无意义。在这一点上我一无所知。
    猜你喜欢
    • 2014-05-03
    • 1970-01-01
    • 2017-05-30
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多