【问题标题】:change classname not work with nextSibling更改类名不适用于 nextSibling
【发布时间】:2011-08-01 10:42:43
【问题描述】:

我的代码如下:

<div class="sidebar" id="southsidebar">

    <!-- archives -->
    <div class="widget">
        <h3>Archives</h3>
        <div class="easy-archives">
            <div class="monthly-archives">
                <div class="month">
                    <div class="title">
<a onclick="toggle(this);" href="javascript:void(0);" class="open-button">toggle</a>                            
                        <a title="Show detailed results for February 2011 " href="#" class="detail">February 2011 </a>                          
                        <em>(2 posts)</em>
                        <div class="fixed"></div>
                    </div>

                    <ul class="open">
                        <li>08: <a title="View this post 1" href="#">Post 1</a><em>(0 comments)</em></li>
                        <li>03: <a title="View this post 2" href="#">Post 2</a><em>(5 comments)</em></li>                       
                    </ul>                               
                </div>
            </div>
        </div>

        <div class="easy-archives">
            <div class="monthly-archives">
                <div class="month">
                    <div class="title">
                        <a onclick="toggle(this);" href="javascript:void(0);" class="open-button">toggle</a>                            
                        <a title="Show detailed results for September 2008" href="#" class="detail">September 2008</a>                          
                        <em>(1 posts)</em>
                        <div class="fixed"></div>
                    </div>

                    <ul class="open">
                        <li>27: <a title="View this post 1"href="#">Post 1</a><em>(277 comments)</em></li>              
                    </ul>                               
                </div>
            </div>
        </div>
    </div>

</div>

<script type="text/javascript">
    function toggle(button) {
        if (button.className == "open-button") {
            button.className = "closed-button";
        } else {
            button.className = "open-button";
        }

        var parent = button.parentNode;
        var dailyArchives = parent.nextSibling;
        if (dailyArchives.className == "open") {
            dailyArchives.className = "closed";
        } else {
            dailyArchives.className = "open";
        }
    }
</script>

<style type="text/css">
.easy-archives .closed {
display:none;
}
</style>

当 nextSibling 不工作时,它与标签一起工作 请有人帮助我。对不起,我的英语不太好。

【问题讨论】:

    标签: toggle classname


    【解决方案1】:

    基本上你想获取div class="month"下的ul标签

    试试这个,

    var parent = button.parentNode.parentNode; //获取 div class="月"
    var dailyArchives = parent.getElementsByTagName('ul').item(0); //获取ul class="open"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-20
      • 1970-01-01
      • 1970-01-01
      • 2015-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多