jquery可折叠默认设置
我试图在一些可扩展div上设置默认值,在设置cookie之前,默认值将被折叠 我在代码中看到一个提示,上面写着“//开发人员指定了什么?折叠还是扩展?” 鉴于它们默认为扩展,我更改了以下内容:jquery可折叠默认设置,jquery,collapse,Jquery,Collapse,我试图在一些可扩展div上设置默认值,在设置cookie之前,默认值将被折叠 我在代码中看到一个提示,上面写着“//开发人员指定了什么?折叠还是扩展?” 鉴于它们默认为扩展,我更改了以下内容: //what has the developer specified? collapsed or expanded? if ( $($(this)).hasClass("collapsed") ) { $("#" + $(this).attr("id") ).next().
//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");
}
还是不走运。清除了我的cookies,但它们仍然默认打开
以下是完整的代码:
/**
* 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++;
});
};
/**
*可折叠插件
*
*版权所有(c)2010 Ramin Hossaini(www.Ramin-Hossaini.com)
*MIT和GPL许可下的双重许可:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
jQuery.collapsable=函数(选择器、标识符){
//在标题后切换div并设置唯一的cookie
$(选择器)。单击(函数(){
$(this).next().slideToggle('fast',function()){
如果($(this).is(“:hidden”)){
$.cookie($(this.prev().attr(“id”),“hide”);
$(this.prev().children(.placeholder”).removeClass(“collapse”).addClass(“expand”);
}
否则{
$.cookie($(this.prev().attr(“id”),“show”);
$(this.prev().children(.placeholder”).removeClass(“expand”).addClass(“collapse”);
}
});
返回false;
}).next();
//显示标题是可单击的
$(选择器).悬停(函数(){
$(this.css(“游标”、“指针”);
});
/*
*在document.ready上:模块应该显示还是隐藏?
*/
var idval=0;//用于生成唯一ID的增量
$。每个($(选择器),函数(){
$($(this)).attr(“id”,“module_u3;”+identifier+idval);//为每个对象指定一个唯一的id
if(!$($(此)).hasClass(“折叠”)){
$(“#”+$(this.attr(“id”)).append(“”);
}
else if($($(this)).hasClass(“折叠”)){
//默认情况下,此文件应折叠
$(“#”+$(this.attr(“id”)).append(“”);
}
//开发人员指定了什么?折叠还是扩展?
if($($(this)).hasClass(“折叠”)){
$(“#”+$(this.attr(“id”)).next().hide();
$(#“+$(this).attr(“id”).children(“span”).removeClass(“collapse”).addClass(“expand”);
}
否则{
$(#“+$(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(“占位符”).removeClass(“expand”).addClass(“collapse”);
}
idval++;
});
};
我猜是关于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>
$('#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');
}
});