2011-03-31 70 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右上角)