2013-10-10 131 views
1

如果您單擊紅色塊顯示的頂部輸入。我怎樣才能阻止它,當你點擊紅色塊或其輸入,然後塊隱藏。我的代碼只適用於Chrome,但不適用於Firefox或ie。可以請你幫如何停止模糊關閉塊

http://jsfiddle.net/n9Mbh/

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <title></title> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 

     <script src="js/libs/jquery-1.8.2/jquery.min.js"></script> 
     <script type="text/javascript"> 

      $(document).ready(function() { 
       var $txtMain = $('#txtMain'); 
       var $popupInput = $('#popupInput'); 
       var $popup = $('#popup'); 



       $popup.hide(); 

       $txtMain.focus(function() {$popup.show();}); 
       $popupInput.focus(function() {$popup.show();}); 
       $popup.focus(function() {$popup.show();}); 

       $txtMain.blur(function() {$popup.hide();}); 
       $popupInput.blur(function() {$popup.hide();}); 
       $popup.blur(function() {$popup.hide();}); 


      }); 
     </script> 
    </head> 
    <body> 

     <br /><br /> 


     <input type="text" id="txtMain" /> 

      <div id="popup" style="width: 200px; height: 200px; background: none repeat scroll 0% 0% red;" tabindex="5"><br /><br /> 
       <input type="text" id="popupInput"> 
      </div> 


       <br /><br /> 
       <br /><br /> 
       <br /><br /> 
       <br /><br /> 
       <br /><br /> 
       <br /><br /> 
       <br /><br /> 

     <input type="text" id="test" /> 
</body> 
</html> 
+0

在Firefox中,當您點擊$ popupInput時,您首先會對剛剛離開的任何輸入發出模糊處理,以便隱藏。這可以防止重點放在$ popupInput上。或者說,它可能會引發新元素的焦點,然後模糊舊元素 – ediblecode

+0

你自己的問題的重複[div沒有得到及時的焦點:模糊在焦點之前觸發](http://stackoverflow.com/questions/19270504/div-is-not-getting-focus-in-time-blur-fires-the-the-focus-out) – CBroe

+0

我從來沒有得到答案 –

回答

0

1)問題是下面的線

$txtMain.blur(function() {$popup.hide();}); 

一旦事件的內容的$txtMain,彈出的是獲得隱藏。

試試這個

$txtMain.blur(function() { 
        if (!$('#popup').is(':visible')) { 
         $popup.hide(); 
        } 
       }); 

入住這Fiddle在Firefox

希望你能理解背後的邏輯。

+0

爲什麼你會有空的if語句?爲什麼不反轉它,並有$ popup.hide()? – ediblecode

+0

@danrhul首先我想展示一個OP來解決問題的例子,如果(!$('#popup')。is(':visible')){popuphide(); }添加一個否定不是一件大事。無論如何,我喜歡這樣做。讓我更新答案。 – Praveen

+0

感謝您的回覆 - 但現在,如果我點擊除$ txtMain,$ popupInput,$ popup之外的任何其他元素,我該如何關閉紅色塊。非常感謝您的幫助,如果可能的話,我不想綁定任何事件。 –