隐藏div - 为什么它不起作用?

时间:2013-09-14 21:24:32

标签: jquery css cookies

我正在尝试使用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

演示

我确定我错过了什么。任何帮助将非常感激。 * CODE REFRESHED * 是否与function cookiepopup_show()有关?

3 个答案:

答案 0 :(得分:2)

将ID #cookiepopup更改为班级.cookiepopup,然后您的CSS规则才能正常运行。

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

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

答案 1 :(得分:2)

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

答案 2 :(得分:2)

您正在以错误的方式使用idclassid应该是唯一的,classes不必是{{1}}。查看(简化的)工作jsFiddle here