简体   繁体   English

Jquery保留点击的链接并在刷新后添加/删除css类

[英]Jquery to retain the links clicked and add/remove css class after refresh

I have a page with multiple "Filters" which are basically links.我有一个包含多个“过滤器”的页面,它们基本上是链接。 Upon clicking the link, I am able to filter the results.单击链接后,我可以过滤结果。 However, I would like to add/remove class upon refresh of page when the link is clicked.但是,当单击链接时,我想在刷新页面时添加/删除类。 I tried using localstorage but my localStorage returns null.我尝试使用 localstorage 但我的 localStorage 返回 null。 Any help would be appreciated.任何帮助,将不胜感激。 I am very new to Jquery.我对 Jquery 很陌生。 It is probably a repeat but my situation seems to be unique as localstorage on refresh is returning null on console.这可能是重复,但我的情况似乎是独一无二的,因为刷新时的 localstorage 在控制台上返回 null。

if (localStorage.getItem('filters')==null){
var filters={};
alert("in here");
}
else{
filters= JSON.parse(localStorage.getItem('filters'));
}

$(".li-filter").click(function(){

/*event.preventDefault();*/
var idstr= this.id.toString();
/*console.log(idstr);*/
if (filters[idstr] != undefined){
  filters[idstr] = filters[idstr]  +1;
}
else {
  filters[idstr]  = 1;
}
localStorage.setItem('filters',JSON.stringify(filters));
console.log(filters);
});

I am not sure if I need to use AJAX to retain my selected link ids.我不确定是否需要使用 AJAX 来保留我选择的链接 ID。

I have a working fiddle based off of your description here : https://jsfiddle.net/7mtxw2a3/4/ .根据您的描述,我有一个工作小提琴: https : //jsfiddle.net/7mtxw2a3/4/ I'd suggest making use of your browser's developer tools to verify the contents of your local storage after setting a value and then again on page refresh.我建议使用浏览器的开发人员工具在设置值后验证本地存储的内容,然后在页面刷新时再次验证。

$(document).ready(function() {
  var cached = localStorage.getItem('filters');
  var filters = cached ? JSON.parse(cached) : {};
  for (id in filters) {
    $('#' + id).addClass('cached');
  }
  $('li').click(function() {
    $(this).addClass('cached');
    var id = this.id.toString();
    if (filters[id]) {
        filters[id] += 1;
    } else {
        filters[id] = 1;
    }
    localStorage.setItem('filters', JSON.stringify(filters));
  });
});

I modified the other answer until it appeared to be working.我修改了另一个答案,直到它看起来有效。

$(document).ready(function() {
  var cached = localStorage.getItem('filters');
  var filters = (cached) ? JSON.parse(cached) : {};
  for (id in filters) {
    $('#' + id).addClass('cached');
  }
  $('li').click(function(e) {
    $(e.target).addClass('cached');
    var id = $(e.target).attr('id').toString();
    if (filters[id]) {
        filters[id] += 1;
    } else {
        filters[id] = 1;
    }
    localStorage.setItem('filters', JSON.stringify(filters));
  });
});

The place to get your clicked object is e.target .获取点击对象的位置是e.target Here's a link to the anatomy of a Jquery event object .这是Jquery 事件对象剖析的链接。

However, when using JQuery and events together, there are a lot of situations where you will need to feed an element back into the selector as I've done with $(e.target) .但是,当一起使用 JQuery 和事件时,在很多情况下,您需要将元素反馈到选择器中,就像我对$(e.target)所做的那样。

And to ask for the id attribute, you need then to chain the .attr() method .要请求id属性,您需要链接.attr()方法

Hat tip to @Freddy for getting the fiddle started.给@Freddy 的小费,让他们开始小提琴。 Here's my version of the fiddle .这是我的小提琴版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM