jquery可折叠默认设置

时间:2012-02-01 13:36:54

标签: jquery collapse

我试图在默认情况下将某些可扩展div上的默认设置折叠,直到设置cookie为止。

我在代码中看到一条提示“//开发人员指定了什么?折叠或扩展?”

看到他们默认扩展的方式我改变了这个:

    //what has the developer specified? collapsed or expanded?
    if ( $($(this)).hasClass("collapsed") ) {
        $("#" + $(this).attr("id") ).next().hide();
        $("#" + $(this).attr("id") ).children("span").removeClass("collapse").addClass("expand");
    }
    else {
        $("#" + $(this).attr("id") ).children("span").removeClass("expand").addClass("collapse");
    }

到此:

    //what has the developer specified? collapsed or expanded?
    if ( $($(this)).hasClass("expand") ) {
        $("#" + $(this).attr("id") ).next().hide();
        $("#" + $(this).attr("id") ).children("span").removeClass("expand").addClass("collapse");
    }
    else {
        $("#" + $(this).attr("id") ).children("span").removeClass("collapse").addClass("expand");
    }

仍然没有运气。清除我的饼干,他们仍然默认打开。

这是完整的代码:

/**
 * Collapsible plugin
 *
 * Copyright (c) 2010 Ramin Hossaini (www.ramin-hossaini.com)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

jQuery.collapsible = function(selector, identifier) {

    //toggle the div after the header and set a unique-cookie
    $(selector).click(function() {
        $(this).next().slideToggle('fast', function() {
            if ( $(this).is(":hidden") ) {
                $.cookie($(this).prev().attr("id"), 'hide');
                $(this).prev().children(".placeholder").removeClass("collapse").addClass("expand");
            }
            else {
                $.cookie($(this).prev().attr("id"), 'show');
                $(this).prev().children(".placeholder").removeClass("expand").addClass("collapse");
            }
        });
        return false;
    }).next();


    //show that the header is clickable
    $(selector).hover(function() {
        $(this).css("cursor", "pointer");
    });

    /*
     * On document.ready: should the module be shown or hidden?
     */
    var idval = 0;  //increment used for generating unique ID's
    $.each( $(selector) , function() {

        $($(this)).attr("id", "module_" + identifier + idval);  //give each a unique ID

        if ( !$($(this)).hasClass("collapsed") ) {
            $("#" + $(this).attr("id") ).append("<span class='placeholder collapse'></span>");
        }
        else if ( $($(this)).hasClass("collapsed") ) {
            //by default, this one should be collapsed
            $("#" + $(this).attr("id") ).append("<span class='placeholder expand'></span>");
        }

        //what has the developer specified? collapsed or expanded?
        if ( $($(this)).hasClass("collapsed") ) {
            $("#" + $(this).attr("id") ).next().hide();
            $("#" + $(this).attr("id") ).children("span").removeClass("collapse").addClass("expand");
        }
        else {
            $("#" + $(this).attr("id") ).children("span").removeClass("expand").addClass("collapse");
        }


        if ( $.cookie($(this).attr("id")) == 'hide' ) {
            $("#" + $(this).attr("id") ).next().hide();
            $("#" + $(this).attr("id") ).children("span").removeClass("collapse").addClass("expand");
        }
        else if ( $.cookie($(this).attr("id")) == 'show' ) {
            $("#" + $(this).attr("id") ).next().show();
            $("#" + $(this).attr("id") ).children(".placeholder").removeClass("expand").addClass("collapse");
        }


        idval++;
    });

};

1 个答案:

答案 0 :(得分:0)

我猜它是关于jQuery Mobile的。 这是我的解决方案。

HTML:

<div data-role="page" id="myPage"><div data-role="content" id="myContent">
<div data-role="collapsible" id="myCollapsible" data-collapsed="false">
   <h3>Collapsible 1</h3>
   <p>Collapsible content 1</p>
</div>
</div></div>

jQuery(asumming你已经加载jquery cookie plugin):

$('#myPage').live('pageshow',function(event){
    //on collapsible click
    $('#myCollapsible').click(function(event){
       //make cookie value collapsible "on"
       $.cookie('collapsible', 'on');
    });

    //verify if cookie exists
    if ($.cookie('collapsible') === 'on') {
          //if cookie exists expand the collapsible
       $('#myCollapsible').trigger('expand');
    }
    else
    {
          //if cookie doesn't exist, collapse the block
       $('#myCollapsible').trigger('collapse');
    }
});

希望它有所帮助,无论如何