2011-03-31 69 views
0

我有一組div,我會在點擊時顯示,然後用右上角的「X」img關閉。我的問題是點擊div內的任何位置會導致它關閉,而不僅僅是右上角的X。我還想讓「X」成爲一個鏈接,因此當它懸停在光標變化上時。代碼如下,謝謝!div上的關閉按鈕無法正常工作

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script> 

<style type="text/css"> 
.county{ 
    color:blue; 
    display:block; 

} 
.countystats{ 
    background-image:url('../defunkt-facebox-cbe32e1/examples/closelabel.png') ; 
    background-position:top right; 
    border:3px black inset; 
    background-repeat:no-repeat; 
    background-color:#ccc; 
    display:none; 
    right:250px; 
    margin: 5px 5px 5px 5px; 
    padding:5px 5px 5px 5px; 
    width:200px; 

} 
</style> 

</head> 

<body> 
<div style="height:250px;bottom:300px; width:100px; padding: 1em; overflow:auto; margin:5px 5px 5px 5px; border: 2px black; overflow-x:hidden;"> 

    <a class="county" href="#">one</a> 
    <a class="county" href="#">two</a> 
    <a class="county" href="#">three</a> 
    <a class="county" href="#">four </a> 
    <a class="county" href="#">five</a> 
    <a class="county" href="#">six</a> 



</div> 
<div class="countystats">stats one<input maxlength="20" size="14" type="password"/><br/><p>woot</p></div> 
<div class="countystats">stats two</div> 
<div class="countystats">stats three</div> 
<div class="countystats">some other stuff</div> 
<div class="countystats">even more other stuff</div> 



<br /> 
<br /> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script> 
<script type="text/javascript"> 
$('a.county').each(function(e){ 
     $(this).bind('click', function(e){ 
      var thisIs = $(this).index(); $('.countystats').eq(thisIs).show (250); 
      }); 
    $("img").hide(); 
    $(".countystats").click(function() { 
     $(this).hide(250); 
     return true;}); 


}); 
</script> 
</body> 
</html> 
+0

你有沒有X的任何一個角落.... – Neal 2011-03-31 20:21:44

+0

在countystats上課有背景圖片。 closelabel.png是我正在談論的X.對不起! – closethemdangdivsright 2011-03-31 20:23:53

回答

3

這是因爲您選擇整個div選擇器的行爲。相反,將一個嵌套div集添加到X圖像的大小,將它浮動到右側,並將關閉事件綁定到該圖像。如果你使用浮動或相對定位,你應該能夠避免弄亂佈局的其餘部分。

編輯:更好的是,嘗試jqueryUI對話框小部件。它或多或少地完全符合您的需求,並自動處理關閉功能。

0

您可以識別單元內發生點擊的位置,並且如果匹配關閉區域的位置,則隱藏該元素。

代碼,這將是:

var _closeButtonWidth = 50; 
var _closeButtonHeight = 20; 

$(".countystats").click(function(event) { 
    event = event || window.event; 
    var xPos = event.offsetX || event.clientX - $(this).offset().left; 
    var yPos = event.offsetY || event.clientY - $(this).offset().top; 
    var width = $(this).width(); 
    var height = $(this).height(); 
    if (xPos >= (width - _closeButtonWidth) && yPos <= (height - _closeButtonHeight)) { 
     $(this).hide(); 
    } 
}); 

只要定義X圖標的寬度和高度,它應該工作。

現場測試案例:http://jsfiddle.net/nmEB6/1/
(測試在Chrome 10,Firefox 3.6及更高IE8只需點擊綠色div右上角)