我想在点击时放置一个 cookie,以便在页面刷新后保持我的选择处于活动状态



我想在页面刷新后点击一个cookie以保持我的选择处于活动状态,我在下面有以下代码:

我尝试了不同的方法,但我无法实现。我是javascript或jquery的菜鸟。

看看我到目前为止尝试过什么。我有一个函数,我想在其中设置 cookie,然后在点击事件时调用该函数。

$(function() {
//LANGUAGE COOKIE
function setLanguageCookie() {
var expire = new Date();
expire = new Date(expire.getTime() + 7776000000);
document.cookie = "Language=Language; expires=" + expire;
}
// THIS IS FOR THE BURGER MENU TOGGLE
$(".dropbtn").click(function() {
$(this).next(".dropdown-content, .items").stop().slideToggle(500);
});
// If you click outside dropdown - close dropdown
var $menu = $('.dropdown');
$(document).mouseup(function(e) {
if (!$menu.is(e.target) && $menu.has(e.target).length === 0) {
$('.dropdown-content').hide();
}
});
$("#dd-content a").click(function(e) {
e.preventDefault();
var text = $(this).text();
var img = $(this).find('img').clone(true);
$('.dropbtn .lang').text(text);
$('.dropbtn img').replaceWith(img);
$("#dd-content a").removeClass('hide');
$(this).addClass('hide');
setLanguageCookie();
});
});
html,
body {
font-family: Lato, sans-serif;
font-size: 14px;
line-height: 1;
background: #191919;
}
header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 5px 15px;
margin: 0 auto 15px auto;
background: #000;
}
header .brand {
font-size: 14px;
line-height: 1;
letter-spacing: 2px;
color: #fff;
}
@media (min-width: 768px) {
header .brand {
font-size: 20px;
}
}
header .logo {
display: none;
}
@media (min-width: 1025px) {
header .logo {
display: block;
}
}
header .dropdown-content {
display: none;
background: #000;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
padding: 10px;
position: absolute;
right: 0;
top: 40px;
}
header .dropdown a.dropbtn {
padding: 0;
display: flex;
align-items: center;
}
header .dropdown a.dropbtn .ico {
margin-left: 5px;
font-size: 11px;
}
header .dropdown {
display: none;
position: relative;
}
@media (min-width: 1025px) {
header .dropdown {
display: initial;
}
}
header .dropdown a {
display: block;
margin: 5px 0 !important;
padding: 3px 5px;
}
header .dropdown-content a.hide {
display: none;
}
header .dropdown a:hover {
color: #999999 !important;
}
header .is-hidden {
display: none;
}
header .actions {
display: flex;
align-items: center;
}
header .actions .dropdown a,
header .actions a.ml,
header .actions a.su {
color: #fff;
text-decoration: none;
margin-left: 20px;
text-transform: uppercase;
}
header .actions a.ml {
display: none;
}
@media (min-width: 1025px) {
header .actions a.ml {
display: block;
}
}
header .actions a.su {
background: #ef2945;
padding: 14px 20px;
border-radius: 4px;
}
header .actions a.su:hover {
background: #df4057;
}
header .menu-mobile .items {
position: absolute;
width: 100%;
top: 52px;
left: 0;
background-color: rgba(21, 21, 21, .98);
z-index: 1;
display: none;
}
header .menu-mobile .items a {
display: flex;
padding: 0 16px;
position: relative;
width: 100%;
border: 0 none;
background: transparent;
height: 36px;
align-items: center;
color: #cbcbcb;
text-transform: uppercase;
border-bottom: 2px solid #0e0e0e;
text-decoration: none;
}
header .menu-mobile .items a span {
margin-right: 10px;
}
header .menu-mobile {
margin-left: 15px;
}
header .menu-mobile a.burger {
color: #fff;
font-size: 17px;
}
header .actions .dropdown a.dropbtn img {
margin-right: 5px;
}
header .actions .dropdown .dropdown-content a {
display: flex;
align-items: center;
}
header .actions .dropdown .dropdown-content a img {
margin-right: 5px;
}
@media (min-width: 1025px) {
.menu-mobile {
display: none;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>
<span class="logo">
<img src="#" width="363" height="24" alt="">
</span>
<div class="brand">MY HEADER</div>
<div class="actions">
<div class="dropdown">
<a href="javascript:void(0)" class="dropbtn">
<img src="assets/img/languages/flag_en.png" alt="">
<span class="lang">EN</span>
<span class="ico ico-pointer_down"></span>
</a>
<div class="dropdown-content" id="dd-content">
<a href="#"><img src="assets/img/languages/flag_br.png" alt=""> PT</a>
<a href="#"><img src="assets/img/languages/flag_es.png" alt=""> ES</a>
<a href="#"><img src="assets/img/languages/flag_fr.png" alt=""> FR</a>
<a href="#"><img src="assets/img/languages/flag_de.png" alt=""> DE</a>
<a href="#"><img src="assets/img/languages/flag_it.png" alt=""> IT</a>
</div>
</div>
<a href="#" class="ml">login</a>
<a href="#" class="su">Join</a>
<div class="menu-mobile">
<a href="#" class="burger"><span class="ico ico-menu"></span></a>
<div class="items">
<a href="#">
<span class="ico ico-user"></span> member login
</a>
<a href="#">
<span class="ico ico-globe"></span> language
</a>
</div>
</div>
</div>
</header>

https://codepen.io/stefan-iordache/pen/JmdZvQ

如果你是一个标签是一个真正的链接,那么你应该添加一个 preventDefault 调用到你的点击事件。 它将阻止在您设置 cookie 之前执行默认链接函数。

function setLanguageCookie() {
var expire=new Date();
expire=new Date(expire.getTime()+7776000000);
document.cookie="Language=Language; expires="+expire;
}
// THIS IS FOR THE BURGER MENU TOGGLE
$(".dropbtn").click(function() {
$(this).next(".dropdown-content, .items").stop().slideToggle(500);
});
// If you click outside dropdown - close dropdown
var $menu = $('.dropdown');
$(document).mouseup(function(e) {
if (!$menu.is(e.target) && $menu.has(e.target).length === 0) {
$('.dropdown-content').hide();
}
});
$("#dd-content a").click(function(e) {
e.preventDefault(); // changes are HERE <--
var text = $(this).text();
var img = $(this).find('img').clone(true);
$('.dropbtn .lang').text(text);
$('.dropbtn img').replaceWith(img);
$("#dd-content a").removeClass('hide');
$(this).addClass('hide');
setLanguageCookie();
});

工作小提琴

您必须在页面加载时获取cookie并将其设置为默认值,例如:

function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
//GET LANGUAGE COOKIE
function getLanguageCookie() {
var lang = getCookie('Language') != ""?getCookie('Language'):"EN";
$('#dd-content').hide();
$("#dd-content a:contains('"+lang+"')").click();
}
//SET LANGUAGE COOKIE
function setLanguageCookie(Language) {
var expire=new Date();
expire=new Date(expire.getTime()+7776000000);
document.cookie="Language="+Language+"; expires="+expire;
}
// THIS IS FOR THE BURGER MENU TOGGLE
$(".dropbtn").click(function() {
$(this).next(".dropdown-content, .items").stop().slideToggle(500);
});
// If you click outside dropdown - close dropdown
var $menu = $('.dropdown');
$(document).mouseup(function(e) {
if (!$menu.is(e.target) && $menu.has(e.target).length === 0) {
$('.dropdown-content').hide();
}
});
$("#dd-content a").click(function() {
var text = $(this).text();
var img = $(this).find('img').clone(true);
$('.dropbtn .lang').text(text);
$('.dropbtn img').replaceWith(img);
$("#dd-content a").removeClass('hide');
$(this).addClass('hide');
setLanguageCookie(text);
});
getLanguageCookie();

最新更新