javascript - div 上的关闭按钮不起作用

标签 javascript jquery html css

我有一组 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/

相关文章:

javascript - 如何使用 JavaScript 附加到 anchor 的 href 属性但不影响查询字符串?

javascript - 在 ExtJS 4 的 tabpanel 中,如何在每次单击选项卡时捕获 tabchange 事件?

jquery - 使用 Javascript 和 jQuery : Escaping quotes and double quotes automatically? 进行清理

javascript - 在 bPopup 中加载图像

javascript - 从 jQuery 中的 <object> 获取 "data"属性

javascript - javascript中千位分隔符分割数字

javascript - 如何同步链式 jQuery Promise 的顺序

html - 为什么元素不显示内联?

Javascript Canvas 绘图 - 大量运动时像素化

javascript - 获取动态生成的下拉列表 onchange 和重用功能的文本