本文介绍了我想在点击时放置一个cookie,以便在页面刷新后保持我的选择处于活动状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在点击后点击一个cookie,以便在页面刷新后保持我的选择活动,并且我在下面有以下代码:
I want to put a cookie on click to keep my selection active after the page refresh and I have this code below:
我尝试了不同的方法,但我可以没有成功。我是javascript或jquery的菜鸟。
I have tried different methods but I can't make it happen. I am a noob at javascript or jquery.
看看我到现在为止做了什么。我有一个函数,我想设置cookie,然后我在点击事件上调用该函数。
Look what I've tried till now. I have a function where I want to set the cookie and then I call that function on click event.
$(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>
推荐答案
。
Working fiddle.
您必须在页面加载时获取Cookie并将其设置为默认值,如:
You've to get the cookies when the page loads and set it as the default like :
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();
这篇关于我想在点击时放置一个cookie,以便在页面刷新后保持我的选择处于活动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!