Cookie Bar本地无法显示求助:JSFiddle正常但本地测试失效
嘿,我看你在本地测试Cookie Bar时遇到了问题,但JSFiddle里能正常运行,这大概率是本地环境的依赖或配置问题,我帮你梳理下可能的原因和解决办法:
核心问题:缺少jQuery Cookie插件
你代码里用到了$.cookie()和$.removeCookie()方法,但原生jQuery并没有这些API——JSFiddle里可能你默认引入了这个插件,但本地只加了jQuery,所以功能直接失效了。
解决步骤:
引入jQuery Cookie插件
先下载jQuery Cookie插件,然后在引入jQuery之后添加这个插件的脚本:<script src="jquery-2.2.4.min.js"></script> <script src="jquery.cookie.min.js"></script> <!-- 新增这一行 -->调整本地测试环境
如果你是直接用file://协议打开本地HTML文件,浏览器出于安全策略可能会限制Cookie存储。建议用本地服务器来测试:- 比如用Python启动简易服务器:在文件目录下运行
python -m http.server 8000,然后访问http://localhost:8000/你的文件名.html - 或者用XAMPP、WAMP这类工具搭建本地服务
- 比如用Python启动简易服务器:在文件目录下运行
整理代码结构
把HTML、CSS、JS分开写,更清晰易维护,下面是完整的可运行代码:
HTML部分
<div id="header"> <h2>Cookie Notification demo</h2> </div> <div id="contentArea"> <input id="clearCookie" type="button" value="Delete Cookie" /> </div> <div id="notification" class="cookie-wrapper cookie hidden"> <p>This site uses cookies! <a id="closeCookie" class="btn" href="#">Close</a></p> </div> <script src="jquery-2.2.4.min.js"></script> <script src="jquery.cookie.min.js"></script> <script> // JS代码放在这里 </script>
CSS部分
.hidden { display:none; } .cookie { bottom: -20px; } .cookie-wrapper { font-size: 12px; font-family: Arial, Helvetica, sans-serif; line-height: 150%; padding: 15px 45px 30px 15px; text-align: left; position: fixed; left: 0; background: #de291e; color: #fff; width: 100%; } .btn { display: inline-block; float: right; width: 15%; text-decoration: none; margin-bottom: 0px; margin-right: 30px; font-size: 14px; line-height: 20px; text-align: center; vertical-align: middle; cursor: pointer; text-shadow: 0px 1px 1px rgba(255, 255, 255, 0.75); background-image: -moz-linear-gradient(top, #fff, #e6e6e6); background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#fff), to(#e6e6e6)); background-image: -webkit-linear-gradient(top, #fff, #e6e6e6); background-image: -o-linear-gradient(top, #fff, #e6e6e6); background-image: linear-gradient(to bottom, #fff, #e6e6e6); background-image: repeat-x; border: 1px solid #ccc; border-bottom-color: #b3b3b3; -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px; box-shadow: inset 0px 1px 0px rgba(255, 255, 255, 0.2), 0px 1px 2px rgba(0, 0, 0, 0.05); outline:none !important; padding: 1px 7px !important; } .btn:hover, .btn:focus { color: #333; text-decoration: none; background-position: 0 -15px; -webkit-transition: background-position .1s linear; -moz-transition: background-position .1s linear; -o-transition: background-position .1s linear; transition: background-position .1s linear; } .btn:hover, .btn:focus, .btn:active, .active.btn, .disabled.btn, [disabled].btn { background-color: #e6e6e6; }
JS部分
var clearCookie = function () { var result = $.removeCookie('JSFiddleCookieNotification'); if (result) { alert('Cookie removed, please reload.'); } else { alert('Error: Cookie not deleted'); } } var closeCookie = function () { $("#notification").toggle("slide", { direction: "down" }); $.cookie('JSFiddleCookieNotification', 'Notified', { expires: 7 }); } // Bind the buttons $("#clearCookie").on("click", clearCookie); $("#closeCookie").on("click", closeCookie); // Now display the cookie if we need to if ($.cookie('JSFiddleCookieNotification') == null) { $("#notification").effect({ effect: "slide", direction: "down", duration: 600 }); }
额外检查点
- 确认浏览器允许本地Cookie:在浏览器设置里检查是否禁用了Cookie,或者对本地文件的Cookie有特殊限制
- 打开浏览器控制台(F12)查看是否有报错,比如
$.cookie is not a function这类错误,能直接定位问题
内容的提问来源于stack exchange,提问作者James




