简体   繁体   English

存在cookie时隐藏div元素

[英]Hide div element when cookie is present

I am trying to hide a popup that keeps appearing everytime a page is loaded. 我试图隐藏一个弹出窗口,该弹出窗口在每次加载页面时都会不断出现。 The plugin is not working out of the box. 该插件无法立即使用。

Our site is: https://www.prikkabelled.nl/ 我们的网站是: https : //www.prikkabelled.nl/

A cookie is loaded into the browser after the user reads the popup message. 用户阅读弹出消息后,会将cookie加载到浏览器中。 The cookie is called pum-10366 with its value set to true. 该cookie称为pum-10366 ,其值设置为true。

So if the cookie is present with indexOf > -1, I don't want that popup to appear anymore. 因此,如果cookie的indexOf> -1存在,我不希望该弹出窗口再出现。

This is what I've got so far: 到目前为止,这是我得到的:

jQuery( document ).ready(function() {
        if (document.cookie.indexOf("pum-10366") !== -1) {
           jQuery('#pum-10366').css('display', 'none !important');
        }
});

The pum element is that large popup that displays once. pum元素是只显示一次的大弹出窗口。 Any suggestions? 有什么建议么?

To fix this you need to remove the !important tag as jQuery doesn't understand it, therefore it's stopping the display from working: 要解决此问题,您需要删除!important标记,因为jQuery无法识别它,因此它使显示停止工作:

jQuery( document ).ready(function() {
    if (document.cookie.indexOf("pum-10366") !== -1) {
       jQuery('#pum-10366').css('display', 'none');
    }
});

If you did need to include the !important tag for a reason you could toggle as class with importance already on: 如果出于某种原因确实需要包含!important标记,则可以将具有重要意义的类切换为:

CSS 的CSS

  .important { display: none !important; }

JQUERY JQUERY

jQuery("#pum-10366").toggleClass("important");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM