2013-05-29 90 views
0

我有一个Superfish菜单项有2个文本输入,如果其中一个字段具有用户焦点,我想确保菜单不会被关闭(隐藏)。停止隐藏事件

我有这一切,除了我不知道如何停止Superfish Hide事件执行。

jQuery(function() { 
     jQuery('ul.sf-menu').superfish({ 
      onBeforeHide: function() { 
       $('ul.sf-menu').find('input[type=text], input[type=password]').each(function() { 
        if ($(this).is(':focus')) { 
         //need code to stop Superfish Hide execution here 
        } 
       }); 
      }, 
      delay: 500 
     }); 
    }); 

如何停止hide事件的执行?

+0

我从来没有与快鱼工作过,但我的猜测是,返回'FALSE' – cfs

+0

@cfs:只会停止当前函数,但不会执行实际隐藏的父函数。 – Victor

+0

发生什么事件?它是否关闭了鼠标,但光标仍在文本框中?答案可能不同。 –

回答

0

嗯,这是固定的。我不得不改变插件代码本身:

if ($ul.find('input[type=text]:focus, input[type=password]:focus ').length == 0){ 

    $ul.stop(true, true).animate(o.animationOut, speed, function() { 
    var $this = $(this);       
    o.onHide.call($this);       
      }); 
    } 
else{ 
     $(this).addClass(o.hoverClass); 
    } 

的想法是计算的一定数量的具有用户的焦点的那一刻,如果有多于一个文本输入框,添加hoverClass这使得菜单项保持可见。如果没有重点项目,它将照常进行隐藏。

1

你需要像event.PreventDefault()这样的东西吗? http://api.jquery.com/event.preventDefault/

或者如果这对于工作来说不够人,return false;应该停止一切工作。

[编辑]你可以尝试做的动画一个.stop()为元素

jQuery(function() { 
    jQuery('ul.sf-menu').superfish({ 
     onBeforeHide: function (liElement) { 
      $('ul.sf-menu').find('input[type=text], input[type=password]').each(function() { 
       if ($(this).is(':focus')) { 
        $(liElement).stop(); 
       } 
      }); 
     }, 
     delay: 500 
    }); 
}); 

或者,如果不帮助你可能不得不取消MouseLeave事件,对于其li元素菜单寄存器。请原谅if

$("ul.sf-menu").on("mouseleave", "li:having(ul)", function(){ 
     $(this).find('input[type=text], input[type=password]').each(function() { 
      if ($(this).is(':focus')) { 
       event.preventDefault(); 
      } 
     }); 
}); 
+0

它似乎只是停止我的'onBeforeHide'函数,但仍然实际隐藏菜单项 – Victor

+0

如果你允许一个参数进入你的函数,然后调用'$(param).stop();''return false ;'? – Klors

+1

实际的隐藏事件是一个'menu.on(“mouseleave.superfish”,“li:having(ul)”,function(){})'所以你可以尝试用你自己的mouseleave函数取消它。 – Klors

0

我有和superfish 1.7.4一样的需求。 这里是我的解决方案,使用Chrome 31,IE 10和IE 10兼容模式的IE 7测试:

/*to store the input which gets the focus (mouse click or tab key)*/ 
    var inputFocused = null; 
    $('#mymenu').superfish({ 
    delay: 500 
    , speed: 'fast' 
    , disableHI: true 
    , onInit: function(){ 
     var ul = $(this); 
     var inputs = ul.find('input[type=text], input[type=password]'); 
     inputs.each(function(index, elt){ 
     $(elt).on('click', function(event){ 
      inputFocused = $(elt); 
      event.stopPropagation(); 
     }); 
     $(document).on('click', function(event){ 
          /*to allow people to choose to quit the menu*/ 
      inputFocused = null; 
     }); 
     $(elt).on('keyup', function(event){ 
      inputFocused = $(elt); 
      event.stopPropagation(); 
     }); 
    }); 
} 
, onHide: function(){ 
var ul = $(this); 
if(inputFocused != null && ul.find(inputFocused).length > 0){ 
    ul.css('display', 'block'); 
    inputFocused.focus(); 
} 
} 
});