javascript - 如何显示和隐藏内部 div onclick 最多 3 个级别?

标签 javascript jquery html css

我的页面结构如下

HTML:

        <div style="position:relative; width:200px; height:100px; overflow:hidden;">
                <div style="position:absolute;" id="innerDiv">
                    <table style="width:400px;height:50px;" border="1" id="innerTable">
                        <tr>
                            <td>
                                <div id="td1"class="monthDiv" style="white-space:nowrap;">
                                    2000
                                </div>
                                <div id="td2" style="white-space:nowrap;">
                                <table>
                                    <tr><td id="quarter1">JAN-JUN00</td><td id="quarter2">JUL-DEC00</td></tr>
                                </table>
                                </div>
                            </td>

                            <td>
                               <div id="w" style="white-space:nowrap;">
                                    2001
                                </div>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>

JavaScript:

    $("#td1").click(function(){
        $("#td1").hide('slow');
        $("#td2").show('slow');
    });

    $("#td2").click(function(){
        $("#td2").hide('slow');
        $("#td1").show('slow');
    });
    $("#quarter1").click(function(){

        });
$("#quarter2").click(function(){

        });

所以,当我点击 'td1'(2000) 时,我显示的是 'td2'(JAN-JUN00 AND JUL-DEC00)' 反之亦然,但我需要当点击 'quarter1' 时显示另一个 div(JAN00 FEB00 ... JUN00)。此外,我还需要找到在哪个 div 上触发了点击事件,'quarter1''quarter2' 以显示 (JUL00 AUG00 . ..DEC00)

请帮帮我。

最佳答案

您可以简单地在 div 上调用 show 事件以在单击 quarter1 时显示:

$("#quarter1").click(function(){
   alert('quarter1');
   $('#quart1').show('slow');
});
$("#quarter2").click(function(){
   alert('quarter2');
   $('#quart2').show('slow');
});

quart1quart2 是您要显示的 divid

编辑:

添加这个:

$("#td1").click(function(e){
    e.stopPropagation();
    $("#td1").hide('slow');
    $("#td2").show('slow');
});

$("#td2").click(function(e){
    e.stopPropagation();
    $("#td2").hide('slow');
    $("#td1").show('slow');
});

关于javascript - 如何显示和隐藏内部 div onclick 最多 3 个级别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18462145/

相关文章:

javascript - 在 HTML 和 CSS 中,我可以在聚焦于文本框时更改父 DIV 的背景吗?

Javascript动态按钮通过id删除项目并删除自己(无限循环)

jquery - 一个 CSS 过渡不稳定,而另一个则工作正常

jquery - 响应式列宽高尺寸

javascript - DIV 的可编辑内容无法正常工作

java - Spring MVC + jQuery 数据表 + JSON 响应

javascript - Bootstrap 工具提示和选择下拉重叠问题

.net - 样式表加载问题

javascript - 使用 Javascript 的 HTML 中的下拉问题

javascript - 创建动态字符串模板的最佳方法是什么