这是我的HTML代码:
<div class="accordion_container">
<div class="accordion_head" node-tag="#1">First Accordian Head<span class="plusminus">+</span>
</div>
<div class="accordion_body">
<p>First Accordian Body, it will have description</p>
</div>
<div class="accordion_head" node-tag="#2">Second Accordian Head<span class="plusminus">+</span>
</div>
<div class="accordion_body">
<p>Second Accordian Body, it will have description</p>
</div>
<div class="accordion_head" node-tag="#3">Third Accordian Head<span class="plusminus">+</span>
</div>
<div class="accordion_body">
<p>Third Accordian Body, it will have description</p>
</div>
</div>
这是我的js脚本
$(document).ready(function () {
//toggle the component with class accordion_body
$(".accordion_head").click(function () {
var tag = $(this).attr("node-tag");
if ($(this).next(".accordion_body").is(':visible')) {
$(this).next(".accordion_body").slideUp(300);
$(this).next(".accordion_body").addClass("collapsed");
Cookies.set('collapsed_Nodes', (Cookies.get('collapsed_Nodes') || '')+tag, { expires: 7, path: '' });
$(this).children(".plusminus").text('+');
} else {
$(this).next(".accordion_body").slideDown(300);
$(this).next(".accordion_body").addClass("expadned");
Cookies.remove('collapsed_Nodes', (Cookies.remove('collapsed_Nodes') || '')+tag, { expires: 7, path: '' });
$(this).children(".plusminus").text('-');
}
});
});
我的问题是,如果我点击折叠,它就会起作用,但是当我点击“扩展”时,它无法从Cookie中删除该值,它只会将值添加到Cookie中...
有人可以帮我找到这个问题吗?
以下是js fiddle链接
答案 0 :(得分:1)
您可以简单地从仍应存在的所有值重建标记,而不是删除标记。这样每次都遵循相同的逻辑。
$(document).ready(function() {
var $accordionHead = $(".accordion_head");
//toggle the component with class accordion_body
$accordionHead.on("click", function() {
var $head = $(this);
$head.toggleClass("collapsed");
if ($head.is(".collapsed")) {
$head.next(".accordion_body").slideUp(300);
} else {
$head.next(".accordion_body").slideDown(300);
}
var $collapsedHeads = $accordionHead.filter(".collapsed");
Cookies.set(
"collapsed_Nodes",
$collapsedHeads.map(function(){ return this.getAttribute("node-tag"); }).get().join(''),
{
expires: 7,
path: ''
}
);
});
});
&#13;
.accordion_container {
width: 500px;
}
.accordion_head {
background-color: skyblue;
color: white;
cursor: pointer;
font-family: arial;
font-size: 14px;
margin: 0 0 1px 0;
padding: 7px 11px;
font-weight: bold;
}
.accordion_body {
background: lightgray;
}
.accordion_body p {
padding: 18px 5px;
margin: 0px;
}
.plus,
.minus {
float: right;
}
.collapsed .minus,
.plus {
display: none;
}
.collapsed .plus {
display: inherit;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-cookie/2.2.0/js.cookie.min.js"></script>
<div class="accordion_container">
<div class="accordion_head" node-tag="#1">First Accordian Head<span class="plus">+</span><span class="minus">-</span>
</div>
<div class="accordion_body">
<p>First Accordian Body, it will have description</p>
</div>
<div class="accordion_head" node-tag="#2">Second Accordian Head<span class="plus">+</span><span class="minus">-</span>
</div>
<div class="accordion_body">
<p>Second Accordian Body, it will have description</p>
</div>
<div class="accordion_head" node-tag="#3">Third Accordian Head<span class="plus">+</span><span class="minus">-</span>
</div>
<div class="accordion_body">
<p>Third Accordian Body, it will have description</p>
</div>
</div>
&#13;
答案 1 :(得分:1)
我已经根据accordion_body元素的hide
类创建了一个代码示例。
它将始终根据隐藏的折叠元素编写值,而不是读取cookie并操纵其所选元素的值。
当没有隐藏任何内容时,它将完全删除cookie。
$(document).ready(function() {
//toggle the component with class accordion_body
$(".accordion_head").click(function() {
var tag = $(this).data("node-tag");
if ($(this).next(".accordion_body").is(':visible')) {
$(this).next(".accordion_body").slideUp(300);
$(this).next(".accordion_body").addClass("hide");
$(this).children(".plusminus").text('+');
}
else {
$(this).next(".accordion_body").slideDown(300);
$(this).next(".accordion_body").removeClass("hide");
$(this).children(".plusminus").text('-');
}
adjustCookie();
});
var adjustCookie = function() {
var tags = [];
$('.accordion_container .hide').each(function() {
var tag = $(this).prev('.accordion_head').data("node-tag");
tags.push(tag.replace('#', ''));
});
if (tags.length) {
Cookies.set('collapsed_Nodes', tags.join('|'), {
expires: 7
});
} else {
Cookies.remove('collapsed_Nodes');
}
}
});
.accordion_container {
width: 500px;
}
.accordion_head {
background-color: skyblue;
color: white;
cursor: pointer;
font-family: arial;
font-size: 14px;
margin: 0 0 1px 0;
padding: 7px 11px;
font-weight: bold;
}
.accordion_body {
background: lightgray;
}
.accordion_body p {
padding: 18px 5px;
margin: 0px;
}
.plusminus {
float: right;
}
.hide {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js"></script>
<div class="accordion_container">
<div class="accordion_head" data-node-tag="#1">First Accordian Head<span class="plusminus">-</span>
</div>
<div class="accordion_body">
<p>First Accordian Body, it will have description</p>
</div>
<div class="accordion_head" data-node-tag="#2">Second Accordian Head<span class="plusminus">-</span>
</div>
<div class="accordion_body">
<p>Second Accordian Body, it will have description</p>
</div>
<div class="accordion_head" data-node-tag="#3">Third Accordian Head<span class="plusminus">-</span>
</div>
<div class="accordion_body">
<p>Third Accordian Body, it will have description</p>
</div>
</div>
答案 2 :(得分:0)
根据documentation,为了删除你刚才需要的coockie:
Cookies.remove('name');
在你的情况下将是:
Cookies.remove('collapsed_Nodes');