2011-05-25 65 views
1

嘿傢伙和女孩! 製作了這個自定義提醒框。如何在點擊時關閉警告框?

<script type="text/javascript"> 
    $(function() { 
     var $alert = $('#alert'); 
     if ($alert.length) { 
      var alerttimer = window.setTimeout(function() { 
       $alert.trigger('click'); 
      }); 
      $alert.animate({ height: $alert.css('line-height') || '80px' }, 200).click(function() { 
       window.clearTimeout(alerttimer); 
       $alert.animate({ height: '0' }, 200); 
      }); 
     } 
    }); 
</script> 

我希望它是開放的,直到用戶選擇點擊它或屏幕上的任何其他地方。我該做什麼?

謝謝你的幫助!

回答

2

假設任意位置單擊(在或出,的alert框本身)應該隱藏/刪除警報:

$('body').click(
    function(){ 
     if ($alert.is(':visible')){ 
      $alert.hide(); 
     } 
    }); 

應該工作,我想。

+0

當我回家時要檢查這一點!感謝名單! – Ismailp 2011-05-26 06:26:51

+0

作品! :) Thanx! – Ismailp 2011-05-26 16:55:49

1

如果您想要擺脫它,請嘗試在觸發onclick事件時調用hide()函數。

$.click(function() { 
    $alert.hide(); 
}); 
0

The top answer here is excellent IMO.佔地面積約其他地方在屏幕上部分。

要做到這一點的方法是將click事件綁定到正文,並停止區域內發生的任何不會觸發關閉警報的事件的傳播。