【问题标题】:Jquery .toggle() with first element openJquery .toggle() 第一个元素打开
【发布时间】:2011-10-06 15:52:24
【问题描述】:

所以我使用 .toggle() 打开和关闭一堆 div,但我在页面加载时打开了第一个 div。问题在于,如果用户单击该 div 以将其关闭,则需要单击两次,第一次单击似乎试图将其再次打开。我该如何处理?

$(document).ready(function(){
    $('.content_accrd').css({"height":"0px"});
    $('.content_accrd:eq(0)').css({"height":"100px"});
    $('.paneltop:eq(0)').css({"background-position":"0px -21px"})
$('.paneltop').toggle( function() {
    $(this).css({"background-position":"0px -21px"})
    .siblings('.content_accrd').animate({"height":"100px"}, 200)
}, function() {
$(this).css({"background-position":"0px 0px"})
    .siblings('.content_accrd').animate({"height":"0px"}, 200);

})
})

标记:

 <div id="panel1" class="panel">
                <h2 class="panel_title">Capad Clients</h2>
                <div class="paneltop">
                </div>
              <div class="content_accrd">
              <p>
text
              </p>
              </div>
          </div>

          <div id="panel1" class="panel">
                <div class="paneltop">
                </div>
              <div class="content_accrd">
              <p>
              text
              </p>
              </div>
          </div>


          <div id="panel1" class="panel">
                <div class="paneltop">
                </div>
              <div class="content_accrd">
              <p>
text
              </p>
              </div>
          </div>

【问题讨论】:

    标签: jquery


    【解决方案1】:

    Example

    编辑:

    $(document).ready(function() {
        $('.content_accrd').css({
            "height": "0px"
        });
        $('.content_accrd:eq(0)').css({
            "height": "100px"
        });
        $('.paneltop:eq(0)').css({
            "background-position": "0px -21px"
        })
        $('.paneltop').click(function() {
            if ($(this).siblings('.content_accrd').height() != 100) {
                $(this).css({
                    "background-position": "0px -21px"
                }).siblings('.content_accrd').animate({
                    "height": "100px"
                }, 200)
            } else {
                $(this).css({
                    "background-position": "0px 0px"
                }).siblings('.content_accrd').animate({
                    "height": "0px"
                }, 200);
            }
        })
    })
    

    【讨论】:

    • 有了这段代码,问题似乎已经从第一个打开的div迁移到了其他div,现在需要点击两次才能激活
    • 我刚刚发布的编辑基于兄弟姐妹的身高进行检查和切换,因为切换对于多个类来说太宽了
    • 好的,很好,工作完美,里面有一些有用的技术,非常感谢!!!
    猜你喜欢
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 2021-04-03
    • 2017-11-20
    • 2013-10-10
    • 1970-01-01
    相关资源
    最近更新 更多