簡體   English   中英

如何在外部單擊時隱藏div

[英]How to hide the div on any click outside

我正在使用jquery並嘗試創建簡單的div隱藏和顯示效果。 效果似乎工作正常,但我需要當用戶單擊文檔的任何其他部分時(即隱藏/顯示框除外),該框應向后隱藏。 這是jsfiddle: http : //jsfiddle.net/39DzJ/ 我沒有正確設置樣式。 我希望效果首先起作用。 誰能幫我嗎 ? 這是HTML代碼:

<style>
#mail_floater
{
    background:#fce8bd;
    height:88px;
    width:342px;
    border:1px solid #b7ad02;
    border-radius:5px;
    position:absolute;
    left:200px;
    top:50px;
    border-top:none;
    z-index:100;
    padding:0;
    }

#subscribe_user
{
    width:248px;
    height:16px;
    border:1px solid #b7ad02;


}

    #cust_care_floater
{
    background:#fce8bd;
    height:12px;
    width:108px;
    border:1px solid #b7ad02;
    border-radius:2px;
    border-bottom-left-radius:2px;
    position:absolute;
    left:450px;
    top:30px;
    border-top:none;
    z-index:100;
    clear:both;
    font-family:Tahoma, Geneva, sans-serif;
    font-size:10px;
    font-weight:bold;
    color:#036f05;
    box-shadow:1px 1px 3px #ccc inset; 
    }

    #closer
{
    float:right;
    margin-right:5px;
    margin-top:2px;
    width:19px;
    height:19px;
    background:url(../images/close.png) no-repeat;

}
</style>
</head>

<body>
<a href="#" id="subscribe">Subscribe</a>
<a href="#" id="customer_care">Customer care</a>
<div id="mail_floater">
    <h5>Email</h5>       
       <div id="closer"></div>
       <div id="email_input"><form><label>Enter E-mail : </label><span><input type="text" id="subscribe_user" /></span>
       <input type="submit" id="subscribe_me" value="Done" /></form></div>

    </div>
     <div id="cust_care_floater">
     <span style="padding:0px 10px 0 10px;">033-993-99920</span>      
</div>
</body>​ 

javascript代碼:

$(document).ready (
                   function()
                   {
                       var disp_box=$('#mail_floater');
                       var sub_link=$('#subscribe');
                       var disp_box_2=$('#cust_care_floater');
                       var sub_link_2=$('#customer_care');

                      disp_box.hide();
                      disp_box_2.hide();

                       sub_link.click
                       (
                           function()
                           {

                                disp_box.show();

                           });

                       disp_box.find('#closer').click
                       (
                           function()
                           {                               
                               disp_box.hide();
                           });


                       sub_link_2.click
                       (
                           function()
                           {

                                disp_box_2.show();

                           });




                   });​
$(document).on('click', ':not(.hideDiv)', function(e){
    if($(e.target).is(':not(.hideDiv)')) { //extra check for good measure
         $('.hideDiv').hide();
    }
});​

演示: http//jsfiddle.net/maniator/S9fDy/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM