使用 cookie 在 jQuery 中关闭后不显示弹出窗口
Not to show popup once closed in jQuery using cookies
我使用下面的代码来关闭后不显示弹出窗口。我尝试了下面的代码,但它不起作用。我检查了 google 但没有找到解决方案。
你能帮我解决这个问题吗?
$(document).ready(function() {
$('.closeme').click(function() {
$('#popup').hide();
if ($.cookie('whenToShowDialog') == null) {
// Create expiring cookie, 2 days from now:
$.cookie('whenToShowDialog', 'yes', {
expires: 2,
path: '/'
});
// Show dialog
$('#popup').show();
}
});
});
#popup {
background-color: #f8f8f8;
padding: 20px;
width: 400px;
position: relative;
}
#popup h2 {
font-size: 25px;
}
.closeme {
position: absolute;
top: 10px;
right: 15px;
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/js/all.min.js"></script>
<div id="popup">
<div class="closeme"><i class="far fa-window-close"></i></div>
<h2>Test popup</h2>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
是的,最后我找到了解决方案,我将display: none
设置为CSS中的#popup
。
首先,它会检查cookies是否真的可用。如果没有,它将显示弹出窗口,一旦您关闭弹出窗口,它将设置 cookie 一天。
$(document).ready(function() {
if ($.cookie('whenToShowDialog') != "true") {
$("#popup").show();
$(".closeme").click(function() {
$("#popup").hide();
// set the cookie for 24 hours
var date = new Date();
date.setTime(date.getTime() + 24 * 60 * 60 * 1000);
$.cookie('whenToShowDialog', "true", {
expires: date
});
});
}
});
#popup {
background-color: #f8f8f8;
padding: 20px;
width: 400px;
position: relative;
display: none;
}
#popup h2 {
font-size: 25px;
}
.closeme {
position: absolute;
top: 10px;
right: 15px;
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/js/all.min.js"></script>
<div id="popup">
<div class="closeme"><i class="far fa-window-close"></i></div>
<h2>Test popup</h2>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
我使用下面的代码来关闭后不显示弹出窗口。我尝试了下面的代码,但它不起作用。我检查了 google 但没有找到解决方案。
你能帮我解决这个问题吗?
$(document).ready(function() {
$('.closeme').click(function() {
$('#popup').hide();
if ($.cookie('whenToShowDialog') == null) {
// Create expiring cookie, 2 days from now:
$.cookie('whenToShowDialog', 'yes', {
expires: 2,
path: '/'
});
// Show dialog
$('#popup').show();
}
});
});
#popup {
background-color: #f8f8f8;
padding: 20px;
width: 400px;
position: relative;
}
#popup h2 {
font-size: 25px;
}
.closeme {
position: absolute;
top: 10px;
right: 15px;
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/js/all.min.js"></script>
<div id="popup">
<div class="closeme"><i class="far fa-window-close"></i></div>
<h2>Test popup</h2>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
是的,最后我找到了解决方案,我将display: none
设置为CSS中的#popup
。
首先,它会检查cookies是否真的可用。如果没有,它将显示弹出窗口,一旦您关闭弹出窗口,它将设置 cookie 一天。
$(document).ready(function() {
if ($.cookie('whenToShowDialog') != "true") {
$("#popup").show();
$(".closeme").click(function() {
$("#popup").hide();
// set the cookie for 24 hours
var date = new Date();
date.setTime(date.getTime() + 24 * 60 * 60 * 1000);
$.cookie('whenToShowDialog', "true", {
expires: date
});
});
}
});
#popup {
background-color: #f8f8f8;
padding: 20px;
width: 400px;
position: relative;
display: none;
}
#popup h2 {
font-size: 25px;
}
.closeme {
position: absolute;
top: 10px;
right: 15px;
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/js/all.min.js"></script>
<div id="popup">
<div class="closeme"><i class="far fa-window-close"></i></div>
<h2>Test popup</h2>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>