0

我正在尝试使用jquery-cookie来处理最近的欧盟 cookie 法律。一切正常,但是我想根据不同的设备显示不同的 cookie “警告”消息。

<div class="cookiepopup mobile_no">
        <p>Questo sito utilizza i cookie, per garantire una migliore esperienza di navigazione. Continuando, ne accetti l'utilizzo.<br /> 
        This website uses cookies to improve your user experience. By continuing to browse the site you are agreeing to our use of cookies.<br />
        <a href="/foo" target="_blank"><b>(Dettagli / Details)</b></a> <input type="submit" class="cookieclose" value="Accetto / Continue" /></p>
    </div>

    <div class="cookiepopup mobile_yes">
        <p>Questo sito utilizza i cookie. Continuando, ne accetti l'utilizzo. 
        This website uses cookies. By continuing to browse the site you are agreeing to our use of cookies.<br />
        <a href="/foo" target="_blank"><b>(Dettagli / Details)</b></a> <input type="submit" class="cookieclose" value="Accetto / Continue" /></p>
    </div>

在 style.css 中:

.mobile_yes{ display:none } <--- normal behaviour
.mobile_no{ display:none } <---- it goes under a specific css media query for smartphones

笼统地说,不考虑 CSS 媒体查询,我无法隐藏任何 div。无论我做什么,.mobile_no总是会显示 div 。

/* COOKIE POPUP */
.cookiepopup {
display:none;
background: #eee; border-top:1px solid #ddd; color:#555; 
bottom: 0;
padding: 5px 2% 10px;
position: fixed;
width: 96%;
z-index: 10; 
font-size:0.8em; 
}

.cookiepopup p {
width:90%; margin:0 auto;  max-width: 970px;
}

.mobile_no{ display:none }

JS文件

$(document).ready(function(){

    check_cookie_for_cookiepopup();

});/*ready*/

var cookiepopup_cookie_name = "cookiepopup" ;
var cookiepopup_duration = 60 ; //scadenza in giorni
var show_cookiepopup = '';

function check_cookie_for_cookiepopup()
{ 
  show_cookiepopup = $.cookie( cookiepopup_cookie_name ); 
  if (show_cookiepopup == 'noshow') {

  } else {
    setTimeout(function(){ cookiepopup_show(); },1000);
  }
}

function cookiepopup_show() {
    $('.cookiepopup').show();
    $('.cookiepopup').find('.cookieclose').unbind('click').click(function(){
        $('.cookiepopup').hide();
        show_cookiepopup = 'noshow';
        $.cookie(cookiepopup_cookie_name, show_cookiepopup, { expires: cookiepopup_duration, path: '/' });
        return false;
    });
}

function set_cookie ( cookie_name, cookie_value,
    lifespan_in_days, valid_domain )
{
    // http://www.thesitewizard.com/javascripts/cookies.shtml
    var domain_string = valid_domain ?
                       ("; domain=" + valid_domain) : '' ; 
    document.cookie = cookie_name +
                       "=" + encodeURIComponent( cookie_value ) +
                       "; max-age=" + 60 * 60 *
                       24 * lifespan_in_days +
                       "; " ;

}

演示在http://www.flapane.com/test/test.php

我确定我错过了一些东西。任何帮助将非常感激。 * 代码已刷新 *是否与此有关function cookiepopup_show()

4

3 回答 3

2

将 id 更改#cookiepopup为类.cookiepopup,然后您的 CSS 规则将正常工作。

目前,id 选择器规则比您的类选择器规则更具体。

正如其他人所指出的,id 在页面中应该是唯一的。

于 2013-09-14T21:29:25.067 回答
2

id 应该是唯一的。您有两个具有相同 id 的 div。

于 2013-09-14T21:29:27.143 回答
2

你正在使用idclass错误的方式。ids 应该是唯一的,classes不必是唯一的。在此处查看(简化的)工作 jsFiddle

于 2013-09-14T21:37:56.487 回答