如何突出显示网站上的新项目从jquery上次访问? [英] How to highlight new items on the site since last visit with jquery?

查看:207
本文介绍了如何突出显示网站上的新项目从jquery上次访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网站上有几个div。我想自上次访问以来突出显示新项目。

I have a couple of divs on the site. I'd like to have the new items highlighted since the last visit.

我不想使用php和mySQL,因为:
- 我不想在数据库中存储每个用户的最后访问信息
- 我想要与非注册/非登录用户一起工作
- 主要内容被缓存,所以我不能输出不同的内容给不同的用户。

I don't want to use php and mySQL for this, because: - i don't want to store every users last visit info in the database - i want this to work with non registered/non logged in users too - the main content is cached, so i can't output different content to different users.

我认为这给我一个解决方案:cookies + javascript:
- 检查如果cookie存在
- 如果是高亮新项目(例如添加一个类到div)
- 更新cookie

I think this leaves me one solution: cookies + javascript: - check if cookie exists - if yes highlight new items (example add a class to the div) - update cookie

那么,我该如何做呢?

divs目前使用相同的类,他们没有id,所以如果需要我可以把div的日期,或项目的主要ID。

The divs are currently using the same class, and they have no id, so if needed I can put there the date of the div, or the primary ID of the item.

推荐答案

您可以添加 data-timestamp =[current_timestamp]属性到你的div。然后将最后一次访问的时间戳存储在cookie中,并添加类似这样的类

You can add a data-timestamp="[current_timestamp]" attribute to your divs. Then store the Timestamp of the last visit in a cookie and add a class like this

$('.divClass').filter(function() {
  return $(this).attr("data-timestamp") > "[last_visit_timestamp]";
}).addClass('highlight');

(请参阅 jQuery:选择所有属性大于值的元素

您可以使用以下网址轻松阅读/写入Cookie:
https://github.com/ carhartl / jquery-cookie /

You can easily read/write cookies with: https://github.com/carhartl/jquery-cookie/

这篇关于如何突出显示网站上的新项目从jquery上次访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆