2013-05-29 89 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(); 
} 
} 
});