单击一个按钮,重新加载 window 参数,然后删除按钮

Click a button, reload window with parameters, and remove button

所以我有一个网页,其中 div 覆盖了整个页面,要求客户 select 他们的首选货币。

当他们 select 美国或英国时,页面会重新加载 URL (/?currency=GBP) 中的货币参数,这会更改页面上显示的价格。

然而,无论我尝试什么,我都无法让 div 在页面重新加载时显示 none。

弹出窗口的代码如下:

<div id="floatingBox">
    <div>
        The two buttons go here.
        <a href="/?currency=GBP">UK</a>
        <a href="/?currency=USD">USA</a>
    </div>
</div>

这是 id floatingBox 的样式:

#floatingBox {
    z-index: 39879;
    display: block;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

这是我正在使用的JavaScript:

if (window.location.search.search('GBP')) {
    document.getElementById('floatingBox').display = 'none';
}

if (window.location.search.search('USD')){
    document.getElementById('floatingBox').display = 'none';
}

所以基本上一切正常,除了在用户 select 编辑选项时删除 #floatingDiv。

如何让这个 div 在页面重新加载时和有 URL 参数时不出现?使用 JavaScript 而不是 jQuery.

我认为使用 onclick 和 运行 一个使用 localStorage 的函数并为那个 div 或类似的东西调用样式变量可能更好...

感谢任何帮助:)

编辑

我要测试:

var hide = localStorage.getItem('currChoice') || 0;
if (hide == 1){
    document.getElementById('floatingBox').style.display = "none";
}

function gbpClick(){
    var currChoice = 1;
    localStorage.setItem('currChoice', currChoice);
    location.href='/?currency=GBP';
}

function usdClick(){
    var currChoice = 1;
    localStorage.setItem('currChoice', currChoice);
    location.href='/?currency=USD';
}

处理锚标记的点击事件,在重新加载页面之前关闭您的 div 然后重新加载。

我在主要问题上发布的编辑非常适合我的需要。

这是最终代码: HTML:

<div id="floatingBox">
<div>
The two buttons go here.
<a href="/?currency=GBP">UK</a>
<a href="/?currency=USD">USA</a>
</div>
</div>

风格:

#floatingBox {
    z-index: 39879;
    display: block;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

JavaScript:

var hide = localStorage.getItem('currChoice') || 0;
if (hide == 1){
document.getElementById('floatingBox').style.display = "none";
localStorage.clear();
}

function gbpClick(){
var currChoice = 1;
localStorage.setItem('currChoice', currChoice);
location.href='/?currency=GBP';
}

function usdClick(){
var currChoice = 1;
localStorage.setItem('currChoice', currChoice);
location.href='/?currency=USD';
}

编辑localStorage.clear(); 添加到 if 以确保如果用户将 URL 从 .com/?currency=GBP.com/?currency=USD 更改回 .com/,他们将再次看到弹出窗口。