javascript - 隐藏的 div 会阻止网站上的内容
问题描述
我的网站上有一个 cookie 同意功能,但即使它隐藏了,它也会以某种方式阻止它背后的链接。如何使同意窗口不阻止后面的链接。我有两种情况需要处理:1)用户接受/拒绝同意,div 消失但仍然阻止内容 2)用户在进入网站时已经同意。div 不可见,但会阻止内容。
同意 DIV:
<div class="fixed-bottom p-4">
<div class="toast bg-dark text-white w-100 mw-100" role="alert" data-autohide="false">
<div class="toast-body p-4 d-flex flex-column">
<h4>Cookie Warning</h4>
<p>
This website stores data such as cookies to enable site functionality including analytics and personalization. By using this website, you automatically accept that we use cookies.
Read more about our cookies <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Cookies">HERE</a>
</p>
<div class="ml-auto">
<button type="button" class="btn btn-outline-light mr-3" id="btnDeny">
Deny
</button>
<button type="button" class="btn btn-light" id="btnAccept">
Accept
</button>
</div>
</div>
</div>
</div>
JS。脚本
function setCookie(name, value, days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
function eraseCookie(name) {
document.cookie = name + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}
function cookieConsent() {
if (!getCookie('allowCookies')) {
$('.toast').toast('show')
}
}
try {
$('#btnDeny').click(() => {
eraseCookie('allowCookies')
$('.toast').toast('hide')
})
}
catch (err) {
// Ignore
}
try {
$('#btnAccept').click(() => {
setCookie('allowCookies', '1', 7)
$('.toast').toast('hide')
})
}
catch (err) {
// Ignore
}
// load
cookieConsent()
解决方案
在 Chat 中进行一些调试后,我们发现了潜在的问题:设置 cookie 后,toast 消息被丢弃,但它周围的容器 -<div class="fixed-bottom p-4">
从未被删除,并且在底层内容上不可见。解决方案是在检查 cookie 或设置 cookie 时也将其隐藏:
$('.toast').toast('hide');
$('.toast').parent().hide();
推荐阅读
- python - pyarrow 表过滤——huggingface
- python - 如何从现有列创建新的数据框列并将值从最小到最大排序
- react-native - 如何为用户(React-Native App)提供下拉选项?
- ruby-on-rails - Rails Minitest:尝试使用 assert_select 时出现关于“document_root_element”的错误
- flutter - 颤振类型的轮播 z-index
- r - 如何从正态分布中提取观察值并使用自定义估计器计算平均值,然后在 r 中循环运行此过程
- excel - 如何在 Excel VBA 中为用户窗体和单元格使用嵌套的 For 循环?
- javascript - 如何让作者角色在 Strapi 仪表板上发布内容?
- r - 如何自动生成函数中的组比较以用于 stat_compare_mean?
- python - 用户名/密码登录系统 - 检查用户名与密码的相关性