Heim  >  Fragen und Antworten  >  Hauptteil

javascript - 点击切换问题

下面的toggle只能做显示隐藏的功能吗?
为什么我点击没用的,toggle里面的函数怎么没反映的
我想做点击第一次弹出把p的高度设为30px,第二下为0
<span class="tabsel"></span>


<p class="accessPage"></p>



$(".tabsel").click(function(){
        $(".accessPage").toggle(
            function(){
               $(this).animate({height : "30px"}, 300);
            },function(){
               $(this).animate({height : "0"}, 300);
            }
         );
});
PHP中文网PHP中文网2750 Tage vor281

Antworte allen(3)Ich werde antworten

  • ringa_lee

    ringa_lee2017-04-10 15:24:35

    对于你说点击没有效果,可能是你js代码写在header 里面并且没有用$(function() {}),那样的话在你的事件没有加在dom上。
    另外你的toggle用法也有问题

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Test</title>
    <script src="http://cdn.bootcss.com/jquery/2.1.0-beta3/jquery.min.js"></script>
    </head>
    <body>
    <span class="tabsel">Span</span>
    <br/><br/>
    <p class="accessPage" style="border: 1px solid blue">This is p</p>
    <script>
    $(".tabsel").click(
        function(){
            $(".accessPage").css('height', 30).toggle(300);
        });
    </script>
    </body>
    </html>
    

    Antwort
    0
  • 阿神

    阿神2017-04-10 15:24:35

    是你的callBack函数用错了吧

    Antwort
    0
  • 迷茫

    迷茫2017-04-10 15:24:35

    $(".tabsel").toggle(function(){
        $(".accessPage").animate({height : "30px"}, 300);
    },function(){
        $(".accessPage").animate({height : "0"}, 300);
    });
    

    Antwort
    0
  • StornierenAntwort