如何删除 cookie 在切换中使用 js.cookie & jquery?
How to remove cookie use js.cookie & jquery in toggles?
这是我的 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 fiddlelink
您可以简单地从所有应该仍然存在的值中重建它,而不是删除标签。这样它每次都遵循相同的逻辑。
$(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: ''
}
);
});
});
.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;
}
<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>
根据 documentation ,要删除 cookie,您只需要:
Cookies.remove('name');
在您的情况下将是:
Cookies.remove('collapsed_Nodes');
我已经为 accordion_body 元素创建了一个基于 hide
class 的代码示例。
它不会读取 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>
这是我的 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 fiddlelink
您可以简单地从所有应该仍然存在的值中重建它,而不是删除标签。这样它每次都遵循相同的逻辑。
$(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: ''
}
);
});
});
.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;
}
<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>
根据 documentation ,要删除 cookie,您只需要:
Cookies.remove('name');
在您的情况下将是:
Cookies.remove('collapsed_Nodes');
我已经为 accordion_body 元素创建了一个基于 hide
class 的代码示例。
它不会读取 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>