2013-05-31 53 views
5

我是一個總編程初學者。我一直在尋找答案,但是我發現的答案都沒有解決我的問題。關閉通過點擊它外彈出div

我用[how do I center javascript css popup div, no matter what the screen resolution?]彈出的問題中解釋的div方法。我想知道:是否可以通過點擊以下代碼中的小改動來關閉div?

非常感謝您提前!

我的代碼:

<script type="text/javascript"> 
    function showPopUp(el) { 
     var cvr = document.getElementById("cover") 
     var dlg = document.getElementById(el) 
     cvr.style.display = "block" 
     dlg.style.display = "block" 
     if (document.body.style.overflow = "scroll") { 
      cvr.style.width = "1024" 
      cvr.style.height = "100%" 
     } 
    } 
    function closePopUp(el) { 
     var cvr = document.getElementById("cover") 
     var dlg = document.getElementById(el) 
     cvr.style.display = "none" 
     dlg.style.display = "none" 
     document.body.style.overflowY = "scroll" 
    } 
</script> 
<style type="text/css"> 
    #cover { 
     display:  none; 
     position:  fixed; 
     left:   0px; 
     top:   0px; 
     width:   100%; 
     height:   100%; 
     background:  gray; 
     filter:   alpha(Opacity = 50); 
     opacity:  0.5; 
     -moz-opacity: 0.5; 
     -khtml-opacity: 0.5 
    } 

</style> 

在HTML中,我已經隱藏與IDS 「dialog1」, 「dialog2」 等申報單的數量。當用戶點擊一個鏈接時,DIV顯示出來,並關閉它,我用一個img鏈接:

< a class="close" href="#2" onclick="closePopUp('dialog2');">< img src="/img/close.png" height="30px"></a > 
+1

發佈您的代碼來獲得答案 – Kingk

+0

該代碼是我發佈的鏈接! – Kristine

+0

作爲一名初學者,您需要一種在文檔上註冊鼠標的方法,然後從那裏關閉彈出窗口。沒有你的代碼,只要我可以去 –

回答

8

當您打開彈出窗口,建立高度的無形的div寬度100%,其中謊言在後面你的彈出窗口。

附上一個onclick功能到div:

document.getElementById('invisibleDiv').onclick = function() 
{ 
    document.getElementById('popup').style.display = 'none'; 
} 

希望幫助

+0

非常感謝!我檢查後我會回信! – Kristine

+0

嘿!非常感謝,它工作! 我嘗試了兩種方法,第一種方法是:我爲每個div添加每個closePopUp函數到#cover,但這太長了,你的方式好多了!對不起,現在只能回答! – Kristine

+0

太棒了! 此外,您可以接受/ upvote它。 – SHANK

0

不只是用JavaScript創建一個無形的div:

補充一點:

function getOffset(el) { 
    var _x = 0; 
    var _y = 0; 
    while(el && !isNaN(el.offsetLeft) && !isNaN(el.offsetTop)) { 
     _x += el.offsetLeft - el.scrollLeft; 
     _y += el.offsetTop - el.scrollTop; 
     el = el.offsetParent; 
    } 
    return { top: _y, left: _x }; 
} 

// function to check intersection rectangle <-> point 
function intersects(ax1, ay1, ax2, ay2, px, py) { 
    return !(ax2 < px || ax1 > px || ay2 < py || ay1 > py); 
} 

document.onclick = function(event) { 
    var dialog = document.getElementById('dialog'); 
    var offset = getOffset(dialog); 
    var ax1 = offset.left; 
    var ay1 = offset.top; 
    var ax2 = ax1 + 300 /* dialog width */; 
    var ay2 = ay1 + 300 /* dialog height */; 

    if(!intersects(ax1, ay1, ax2, ay2, event.pageX, event.pageY)) { 
     closePopUp('dialog'); 
    } 
}; 
+0

謝謝,我會檢查它! – Kristine

0

是...重要的是你刪除覆蓋層,那就是<div class="ui-widget-overlay ui-front"></div> w母雞使用Jquery 2.0.0.js ...(這可能是與以前的庫改名字)

例...

<html> 
<head> 
<script type="text/javascript" src="http://code.jquery.com/jquery-2.0.0.js"></script> 

    <script type="text/javascript" src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script> 
<link href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css"/> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
</head> 
    <script type="text/javascript"> 
// <---- VENTAÑAS DE PARAMETERES----> 
$(document).ready(function() { 
$("#wnd_Paramedit").dialog({ 
       autoOpen: false, 
       height: 'auto', 
       width: 405, 
       modal: true, 
       resizable:false, 
       buttons: { 
        "Accept": function() { 


         $(this).dialog("close");  
        }, 
        Cancel: function() { 
         $(this).dialog("close"); 
        } 
       }, 
       close: function() { 
        $(this).dialog("close"); 
       } 
      }); 

      $("#btn_Pedit").click(function() { 
        $("#wnd_Paramedit").dialog("open"); 
$("div").removeClass("ui-widget-overlay ui-front"); 
       }); 
}); 
</script> 
<body> 
<h3>List of parameters</h3> 
<div id="sortparam" > 


</div> 
<input type="button" id="btn_Pedit" value="Edit"/> 
<div id="wnd_EditParam" title="Edit Parameter"></div> 
</body> 
<div id="wnd_Paramedit" title="Choose parameter" > 
    <label> inserta el nombre del Parameter que quiere modificar<label><br /> 
    <label> ID <input type=text size=30 id="med"></label>  
</div> 
</html> 

現在你可以創建接近窗口中的其他特定按鈕彈出,你粘貼接近這個特定代碼:

$("#wnd_Paramedit").dialog("close"); 

$("#wnd_Paramedit").dialog("destroy"); 
0
var $popup = $('#your-popup'); 

$('body').on('click', function(ev) { 
    $popup.hide(); // click anywhere to hide the popup; all click events will bubble up to the body if not prevented 
}); 

$popup.on('click', function(ev) { 
    ev.stopPropagation(); // prevent event from bubbling up to the body and closing the popup 
}); 
相關問題