[英]cookies code help needed (html,css,js)
我对这个脚本感到疯狂。 没用 谁能给我一个如何解决的提示?
我的代码:在我的html中的标记内:
HTML代码:
<div class="cookie-message">
<p class="cookie-content">This website uses cookies. By Browsing this website you acconsent to the use of cookies. </p>
<p class="cookie-content"><button class="button">OK</button> <a href="http://here-goes-my-cookie-page.com">Read more about cookies</a></p>
</div>
我的CSS文件(更多用于样式设置):代码:
.cookie-message {
width: 100%;
background: #333;
color: #ddd;
padding: 1em 1em .5em;
position: fixed;
left: -5px;
bottom: 2em;
text-align: left;
z-index: 1;
}
而我的JavaScript如下:代码:
<!--Start Cookie Script-->
<script>
$('.cookie-header').click(function() {
$('.cookie-content').slideToggle('fast');
$('.cookie-content button').click(function() {
$('.cookie-content').slideUp('fast');
});
});
</script>
<!--End Cookie Script-->
实际上,我是JavaScript的新手,所以我复制了它并将其插入</body>
标记之前,显然它不起作用...
有人能帮我吗? 按下“确定”按钮时,我需要关闭消息...
您在HTML和JQuery选择器中的类名称不匹配。
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
<!-- Stuff -->
$(document).ready(function(){
$('.cookie-message').click(function() {
$('.cookie-content').slideToggle('fast');
$('.cookie-content button').click(function() {
$('.cookie-content').slideUp('fast');
});
});
});
</body>
我相信这就是您正在寻找的JSFiddle示例。
您需要使用准备好的文档包装jQuery函数:
$(document).ready(function(){
$('.cookie-header').click(function() {
$('.cookie-content').slideToggle('fast');
$('.cookie-content button').click(function() {
$('.cookie-content').slideUp('fast');
});
});
});
关于您的代码的另一件事是您没有设置cookie变量,并且用户每次连接到您的网站时都必须选择“确定”。
这是我用于所有项目的代码
$('#cookie_stop').on('click', function(){//#cookie stop is the Id of the button that closes the disclaimer
$('#cookie_disclaimer').slideUp(); //#cookie_disclaimer is the Id of the cookie disclaimer container
//here i set the cookie variable "disclaimer" to true so i can check if the user
var nDays = 999;
var cookieName = "disclaimer";
var cookieValue = "true";
var today = new Date();
var expire = new Date();
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = cookieName+"="+escape(cookieValue)+";expires="+expire.toGMTString()+";path=/";
//done with the cookie
});
因此,当我在HTML中编写Cookie免责声明部分时,会出现类似这样的内容(Php示例)
<?if isset($_COOKIE['disclaimer']){?>
<!--cookie disclaimer part-->
<?}?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.