我有一组 div,我希望通过单击显示这些 div,然后在右上角以“X”img 关闭。我的问题是单击 div 中的任意位置会导致它关闭,而不仅仅是右上角的 X。我还想让“X”成为一个链接,这样当它悬停在光标上时就会发生变化。代码如下,谢谢!
<!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>
最佳答案
之所以如此,是因为您通过选择器选择了整个 div。相反,添加一个嵌套的 div 设置为 X 图像的大小,将其 float 到右侧,并将关闭事件绑定(bind)到它。如果您使用 float 或相对定位,您应该能够避免弄乱布局的其余部分。
编辑:更好的是,试试 jqueryUI 对话框小部件。它或多或少完全符合您的要求,并自动处理关闭功能。
关于javascript - div 上的关闭按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5505791/