在 Stackoverflow 的一些帮助下(我是 JS 的 0 级新手),我实现了一个 div gift 在满足条件时显示:
if(total > 999 && total < 2999) {
$('#gift').show();
然后显示一个工具提示,它将在 6 秒后隐藏:
if(total > 999 && total < 2999) {
$('#gift').show();
$('.tooltip').show();
window.setTimeout(function(){
$('.tooltip').fadeOut('slow');
},6000);
}
好的,运行良好,但我现在感兴趣的是工具提示只向用户显示一次,为此我尝试使用 localStorage,但它对我不起作用:
const showTooltip1=localStorage.getItem('tooltip');
if(showTooltip1==='false'){
$('.tooltip').hide();
}
// The section below is not of my interest in the script, but it may be
/*
$('#gift').on('click',function(){
$('.tooltip').fadeOut('slow');
localStorage.setItem('tooltip','false');
});
*/
我在这里做错了什么......?
这将是主脚本(在每次点击中)为工具提示触发 .show ();
...?
提前致谢!
//------------------------
完整脚本:(工具提示没有本地存储)
$(document).ready(function(){
function manageRegalo() {
var totalStorage = Number(localStorage.getItem("total"));
var total = Number($("#total").val().replace(".",""));
if(totalStorage != null && total === 0) {
total = totalStorage;
}
if(total > 999 && total < 2999) {
$('#gift').show();
$('.tooltip').show();
window.setTimeout(function(){
$('.tooltip').fadeOut('slow');
},6000);
}
else{
$('#gift').hide();
}
}
$(document).on('click', function (event) {
const target = event.target;
if (target.matches('.comp-clone') || target.matches('.bbp')) {
manageRegalo();
localStorage.setItem('total', Number($("#total").val().replace(".","")));
}
});
manageRegalo();
});
最佳答案
您还没有用 setItem
指明您在写什么,所以我们无法知道 getItem
应该返回什么。
您在此之后的逻辑是:如果 getItem
返回 null
,则显示工具提示并使用 setItem
将值放入 localStorage这样后续检查将阻止显示工具提示。
您需要按照以下几行修改您的代码:
if (localStorage.getItem('suppress_gift_tooltip') == null) {
// Your unaltered tooltip code
$('.tooltip').show();
window.setTimeout(function(){
$('.tooltip').fadeOut('slow');
},6000);
// Prevent subsequent display
localStorage.setItem('suppress_gift_tooltip', 'true')
}
关于javascript - 使用 localStorage 隐藏我的工具提示,但在我的脚本中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55870592/