简体   繁体   English

突出显示新项目,直到刷新或下次访问 LocalStorage

[英]Highlighting new items until refresh or next visit with LocalStorage

My task: show newly added and never seen items (by user's browser) in bold And on next visit or page refresh remove the bolding from the items that have already been viewed (until reset button has been clicked).我的任务:以粗体显示新添加和从未见过的项目(通过用户的浏览器)并在下次访问或页面刷新时从已经查看过的项目中删除粗体(直到单击重置按钮)。

So far i was only been able to do it with.click toggle, but can not figure out how to make it with any other method.到目前为止,我只能用.click toggle 来做到这一点,但无法弄清楚如何用任何其他方法来做到这一点。 I Couldn't find a solution for this anywhere on inte.net either.我也无法在 inte.net 上的任何地方找到解决方案。

I'm using jquery and LocalStogage throughout the site and would like to stick to it here as well, if possible.我在整个网站上都使用 jquery 和 LocalStogage,如果可能的话,我也想在这里坚持使用它。

The closest i found to my theme is Detect newly inserted element in the document but it refers to things like MutationObserver that might not be required (as i use unique ID's for each element) and is also bit above my understanding.我发现最接近我的主题的是Detect newly inserted element in the document但它指的是可能不需要的 MutationObserver 之类的东西(因为我为每个元素使用唯一的 ID)并且也有点超出我的理解。 This thread is all about cookies, using which i'd like to avoid: How to highlight new items on the site since last visit with jquery?这个线程是关于 cookies 的,我想避免使用它: 如何突出显示自上次访问 jquery 以来网站上的新项目?

Here's a codepen with working.click toggle event that i'm trying to convert for my task: https://codepen.io/asdanguss/pen/yLqXZBG这是我正在尝试为我的任务转换的带有 working.click 切换事件的 codepen: https://codepen.io/asdanguss/pen/yLqXZBG

<div id="id00001" class="classer viewed">ITEM 1</div><br><br>
<div id="id00002" class="classer viewed">ITEM 2</div><br><br>
<div id="id00003" class="classer viewed">ITEM 3</div><br><br>
<div id="id00004" class="classer viewed">ITEM 4</div><br><br>
<div id="id00005" class="classer viewed">ITEM 5</div><br><br>
<center><button class="reseter" onClick="refreshPage()">Reset</button></center>

body {background: black;}
.classer {position: absolute; padding: 5px; background: #323232; width: 50%; left:25%; color: white; font-weight: 900;}
.viewed {font-weight: 100;}
.reseter {color: blue;}

$(".classer").click( function() {
  $(this).toggleClass('viewed');
  event.preventDefault();
});

$(".classer").click(function(){
                       var btnStorage = $(this).attr("id");
                        if($(this).hasClass("viewed")) {
                            localStorage.setItem(btnStorage, 'true');
                        } else {
                            localStorage.removeItem(btnStorage, 'true');
                        }
                    });

$( ".classer" ).each(function() {
                      var mainlocalStorage = $( this ).attr( "id" );
                      if(localStorage.getItem(mainlocalStorage) == 'true') {
                        $(this).addClass("viewed");
                    } else {
                        $(this).removeClass("viewed");
                    }
                    });

$(".reseter").click(function(){
    localStorage.clear();
});

function refreshPage(){
    window.location.reload();
} 

Thanks for your attention!感谢您的关注!

got it resolved with something like this:用这样的方法解决了它:

<div class="hiss classer"><div id="id00001" class="nju">Item 1</div></div><br><br>
<div class="hiss classer"><div id="id00002" class="nju">Item 2</div></div><br><br>
<div class="hiss classer"><div id="id00003" class="nju">Item 3</div></div><br><br>
<div class="hiss classer"><div id="id00004" class="nju">Item 4</div></div><br><br>
<div class="hiss classer"><div id="id00005" class="nju">Item 5</div></div><br><br>

<center><button class="reseter" onClick="refreshPage()">Reset</button></center>




.classer {position: absolute; background:grey; width: 50%; left:25%; color: white; font-weight: 900;}
.viewed {font-weight: 100;}
.reseter {color: blue;}





$(function(){

$( ".hiss" ).on( "click", function() {
                   var bunStorage = $(this).find(".nju").attr("id");
                    if($(this).hasClass("hiss")) {
                        localStorage.setItem(bunStorage, 'true');
                    } else {
                        localStorage.removeItem(bunStorage, 'true');
                    }
});

$( ".hiss" ).trigger( "click" );

});



$( ".hiss" ).each(function() {
                  var mainlocalStorage = $(this).find(".nju").attr("id");
                  if(localStorage.getItem(mainlocalStorage) == 'true') {
                    $(this).find(".nju").parent().addClass("viewed");
                } else {
                    $(this).find(".nju").parent().removeClass("viewed");
                }

                });





$(".reseter").click(function(){
localStorage.clear();
});


function refreshPage(){
window.location.reload();
} 

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

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