javascript - 如何在文档准备好时使 Jquery 可折叠面板折叠而不是展开?

标签 javascript jquery expand collapsable

我有以下 jquery 代码插件:

           (function ($) {
                $.fn.extend({
                    collapsiblePanel: function () {
                        // Call the ConfigureCollapsiblePanel function for the selected element
                        return $(this).each(ConfigureCollapsiblePanel);
                    }
                });
            })(jQuery);

            function ConfigureCollapsiblePanel() {
                $(this).addClass("ui-widget");

                // Check if there are any child elements, if not then wrap the inner text within a new div.
                if ($(this).children().length == 0) {
                    $(this).wrapInner("<div></div>");
                }

                // Wrap the contents of the container within a new div.
                $(this).children().wrapAll("<div class='collapsibleContainerContent ui-widget-content'></div>");

                // Create a new div as the first item within the container.  Put the title of the panel in here.
                $("<div class='collapsibleContainerTitle ui-widget-header'><div>" + $(this).attr("title") + "</div></div>").prependTo($(this));


                // Assign a call to CollapsibleContainerTitleOnClick for the click event of the new title div.
                $(".collapsibleContainerTitle", this).click(CollapsibleContainerTitleOnClick);
            }


            function CollapsibleContainerTitleOnClick() {
                // The item clicked is the title div... get this parent (the overall container) 
                // and toggle the content within it.
                $(".collapsibleContainerContent", $(this).parent()).slideToggle();
            }

然后这里是html:

<div class="collapsibleContainer" title="Example Collapsible Panel">
                                    <p>
                                        KDLorem ipsum dolor sit amet, consectetur adipiscing elit.... (you get the idea!)
                                    </p>
                                </div>  

然后这里是css:

.collapsibleContainer
{
}

.collapsibleContainerTitle
{
   cursor:pointer;
}

.collapsibleContainerTitle div
{
  padding-top:5px;
  padding-left:10px;
}

.collapsibleContainerContent
{
   padding: 10px;
}

这是文档就绪函数:

jQuery(function () {
   jQuery(".collapsibleContainer").collapsiblePanel();
});

我需要编写什么才能使面板在文档就绪时真正折叠起来,而不是像现在加载文档时那样展开?

最佳答案

将面板的内容设置为display:none

看起来插件使用了 slideToggle(),它只是动画转换到/从 display:none

所以,我认为:

.collapsibleContainerContent
{
   padding: 10px;
   display: none;
}

关于javascript - 如何在文档准备好时使 Jquery 可折叠面板折叠而不是展开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10306580/

相关文章:

jquery - jQuery 中的 .removeProp() 是否存在错误,或者我做错了什么?

html - 我怎样才能在这些列表项中找到一个 anchor 来填充列表项的空间?

ruby-on-rails - 表内/表行之间的 Bootstrap 折叠不起作用

html - 垂直扩展一个 div 容器以适应另一个容器

javascript - 防止脚本根据设备大小运行

javascript - Twitter Bootstrap 预输入 : get context/calling element with `this`

javascript - TypeScript 中 Uint8Array 类型变量的可能值是多少?

javascript - 页面刷新函数的回调

javascript - 确定何时停止修改 div 标签

javascript - ImmutableJS - 只更新对象中的一个键值