【jQuery】示例6:可以伸展的面板

是的,这个是示例6,而不是计划中的示例5(投票系统)。因为在jQuery中,进行可以伸展的面板的处理非常简单,只要一个toggle()函数就可以。 核心代码如下:

var i=0;
$(document).ready(
    function()
    {
        $(div).filter(#clickme).click(
            function()
            {
                i++;
                $(div).filter(#content).toggle();
                if(i%2==0)
                {
                    $(this).text(Click here to expand.);
                }
                else
                {
                    $(this).text(Click here to collapse.);
                }
            }
        );
    }
);

我用了一个变量i来判断要操作的div是处在显示还是隐藏状态,并相应的修改提示文字。通过查看jQuery的源代码,可以知道这也是jQuery中toggle使用的方法。

示例见此处

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *