开发者社区 问答 正文

jquery toggle使用的疑问

 <!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>无标题文档</title>
<script src="../jquery-1.8.3.js"></script>
<script>
$(function(){
    $("div").hover(
        function(){$(this).addClass("over");},
        function(){$(this).removeClass("over")}
    );
    $("div>p").toggle(
        function(){
            $(this).next().toggle(function(){alert("a");},function(){alert("b");});
            },
        function(){
            $(this).next().toggle(function(){alert("c");},function(){alert("d");});
            }
        );
});
</script>
<style>
div p{
    color:green;
}
.over{
    background-color:#C9F;
}
img{
    width:10px;
    height:10px;
}
div.h{
    display:none;
}
div.h1{
    display:block;
}

</style>
</head>

<body>
<div><p><img src="1.jpg"/>1</p><div class="h">AAAAAA</div></div>
<div><p><img src="1.jpg"/>2</p><div class="h">BBBBBB</div></div>
<div><p><img src="1.jpg"/>3</p><div class="h">CCCCCC</div></div>
<div><input type="button" onclick="show()"/></div>
</body>
</html>

为什么我点击标签的时候没有弹出a,b,c,d

展开
收起
小旋风柴进 2016-03-13 11:39:42 1727 分享 版权
1 条回答
写回答
取消 提交回答
  • 你只是给p的兄弟节点div注册了点击事件,div隐藏你有点击不到。。当然没反应了

             $(function () {
                $("div").hover(
                    function () { $(this).addClass("over"); },
                    function () { $(this).removeClass("over") }
                );
                $("div>p").toggle(
                    function () {
                        $(this).next().toggle();////////
                    },
                    function () {
                        $(this).next().toggle();////////
                    }
                    );
            });
    2019-07-17 19:02:21
    赞同 展开评论
问答分类:
问答标签:
问答地址: