gpt4 book ai didi

javascript - 基于 jQuery cookies 打开和关闭面板

转载 作者:行者123 更新时间:2023-12-02 19:26:39 24 4
gpt4 key购买 nike

我有以下变量来存储一组 cookie 的值:

var filterDate = $.cookie('filterDate');
var filterArea = $.cookie('filterArea');
var filterCategory = $.cookie('filterCategory');
var filterType = $.cookie('filterType');
var filterLevel = $.cookie('filterLevel');
var filterAge = $.cookie('filterAge');
var filterAttendance = $.cookie('filterAttendance');

cookie 和变量的名称也是页面上某些元素的 ID,例如:<div id="filterDate"></div>

我想做的是最少(即尽可能少的代码)检查是否有 open 值,如果是,则运行里面的代码。

if (filterDate == 'open' || filterArea == 'open' || filterCategory == 'open' || filterType == 'open' || filterLevel == 'open' || filterAge == 'open' || filterAttendance == 'open') {
$('#' + filter).find('.menuBox.nav ul').show();
$('#' + filter).find('.menuBox.nav p').hide();
$('#' + filter.find('h3 span').addClass('active');
}

如何使上述内容像 filter 一样工作适用于所有 cookie,而无需为每个 cookie 和面板复制它?

最佳答案

紧凑的解决方案可能是:

//The array below allows you to easily add new filters
var filterNames = ["filterDate", "filterArea", ..., "filterAttendance"];
for (var i in filterNames) {
var filterName = filterNames[i];
var filterStatus = $.cookie(filterName);
if (filterStatus == 'open') {
$('#' + filterName).find('.menuBox.nav ul').show();
$('#' + filterName).find('.menuBox.nav p').hide();
$('#' + filterName.find('h3 span').addClass('active');
}
}

关于javascript - 基于 jQuery cookies 打开和关闭面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11970290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com