gpt4 book ai didi

javascript - 我想在点击时放置一个 cookie,以在页面刷新后使我的选择保持事件状态

转载 作者:行者123 更新时间:2023-11-30 06:22:05 25 4
gpt4 key购买 nike

我想在点击时放置一个 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

最佳答案

如果您的 a 标签是一个真正的链接,那么您应该向您的点击事件添加一个 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();
});

关于javascript - 我想在点击时放置一个 cookie,以在页面刷新后使我的选择保持事件状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52577528/

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