[英]Store onclick event in browser with jQuery Cookie
I'm working on a newsfeed of posts that on click filters out the the type of content listed. 我正在处理帖子的新闻提要,这些帖子在单击时会过滤掉列出的内容类型。
<div class="option all" onclick="Stories('all',0);"></div>
<div class="option photos" onclick="Stories('photos',0);"></div>
<div class="option videos" onclick="Stories('videos',0);"></div>
<div class="option music" onclick="Stories('music',0);"></div>
This is the code i use to customize css style for each button and save it. 这是我用于自定义每个按钮的CSS样式并保存它的代码。
$('.option').eq($.cookie('active')).addClass('clicked');
$(".option").click(function (e) {
$.cookie('active', $('.option').index(this));
e.preventDefault();
$(this).toggleClass("clicked");
$(".option").not(this).removeClass("clicked");
});
How can i store the "onclick" event with jquery.cookie.js? 如何使用jquery.cookie.js存储“ onclick”事件? On browser reload the default "all" shows but I would want the last "onclick" option of posts to show instead and not just the css style so I need a way to save the "onclick" 在浏览器上重新加载默认的“全部”显示,但我希望显示帖子的最后一个“ onclick”选项,而不仅仅是css样式,因此我需要一种保存“ onclick”的方法
I recommend use localStorage
instead a cookie : 我建议使用localStorage
代替cookie :
You can handle localStorage
easy without any library and is supported in all modern browsers (IE8 +): 您无需任何库即可轻松处理localStorage
并且所有现代浏览器(IE8 +)都支持该存储:
localStorage.setItem('lastClicked', 'Your value here'); //save
localStorage.getItem('lastClicked'); //retrieve
Check out the documentation . 查看文档 。
Here is the browser support . 这是浏览器支持 。
[Update] - Integrating code with click event [更新]-将代码与click事件集成
$(document).ready(function(){
$('.clicked').click(function(){
localStorage.setItem('lastClicked', this); //replace this for whatever you want to store
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.