开发者社区> 问答> 正文

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 1617 0
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
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载

相关实验场景

更多