重新加载后使用JavaScript操纵的DOM元素操纵或在分页上转到下一页
我正在尝试在我的前端HTML页面上的网格和列表视图模式之间切换一个视图。我可以通过在两个容器之间切换“显示:无”来对DOM和HTML类操作进行操作。但是,当我转到下一个产品页面(通过分页)或重新加载页面时,默认视图是出现的视图,而不是最后一个切换的视图。如果页面重新加载或产品分页发生变化,有没有办法坚持这种视图?谢谢。
这是实现这一目标的DOM代码:
viewList.addEventListener('click', function() {
this.classList.add('view__active');
viewGrid.classList.remove('view__active');
gridItem.classList.add('hidden');
listItem.classList.remove('hidden');
});
viewGrid.addEventListener('click', function() {
this.classList.add('view__active');
viewList.classList.remove('view__active');
gridItem.classList.remove('hidden');
listItem.classList.add('hidden');
});
到目前为止,我发现我必须使用localstorage来实现这一目标。但是有更好的方法可以做到吗?
I am trying to toggle a view between grid and list view mode on my frontend HTML page. I am able to do this fine with dom and HTML classes manipulation by toggling "display: none" between two containers. However, when I go to the next product page(through pagination) or when I reload the page, the default view is the one that appears and not the one that was last toggled. Is there a way to persist the view in case a page reload or product pagination changes? thank you.
here is the dom code that achieves this :
viewList.addEventListener('click', function() {
this.classList.add('view__active');
viewGrid.classList.remove('view__active');
gridItem.classList.add('hidden');
listItem.classList.remove('hidden');
});
viewGrid.addEventListener('click', function() {
this.classList.add('view__active');
viewList.classList.remove('view__active');
gridItem.classList.remove('hidden');
listItem.classList.add('hidden');
});
So far I found that I have to use localStorage to achieve this. but is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
本质上,发生的事情是,当您从服务器请求某些内容时,服务器使用HTML文档响应,并且与该文档关联的任何脚本都运行,因此,在第一个请求中执行的任何JS都不是在第二个请求(paginate或paginate或重新加载)。
因此,您需要一种在这些页面加载中坚持信息的方法,为此,您有3个选项。
使用 sessionstorage 。。
。
使用 localstorage localstorage
使用 cookies 。
最简单的3个是使用选项1或2。
回复您的评论,
我不太清楚您所说的“您用来存储状态的内容”,如果您的问题是关于数据存储的位置,则不必担心它,因为这是由浏览器处理的。如果您的问题是关于如何存储它的“如何存储”,则可以通过选项1或2中的MDN文档。这只是存储键值对,如文档所示
localstorage.setItem('PrefereDview','grid');
您可以按以下单击处理程序将其添加到以下单击“处理程序”中,然后在脚本顶部加载新页面时,您可以获取用户PreveneDview (如果存在)通过
const prefendview = localstorage.getItem('preferedView');
这是一个完整的示例,来自 mdn
Essentially what is happening is when you request something from the server, the server responds with an HTML document, and whichever scripts associated with that document is run, So whatever JS executed in the first request is not in context when the second request(paginate or reload) is made.
So you need a way to persist information across these page loads, For that, you have 3 options.
Use sessionStorage.
Use localStorage
Use Cookies.
Of the 3 above the easiest would be to use either option 1 or 2.
Replying to your comment,
I'm not quite clear as to what you mean by "What you are using to store the state" If your question is about where your data is stored, you need not worry about it as this is handled by the browser. If your question is about "How" to store it you can go through the MDN docs attached in option 1 or 2. This is simply storing a key-value pair as shown in the docs
localStorage.setItem('preferedView', 'grid');
You can add this to your on click handlers as follows,Then when loading a new page at the top of your script you can get the users preferedView(if existing) via
const preferedView = localStorage.getItem('preferedView');
Here is a complete example from MDN
为了让任何人找到类似任务的答案,感谢 @umendra> @umendra Insight,我能够解决通过使用此信息:
我最终在LocalStorage上使用了SessionStorage,因为它可以在窗口/选项卡关闭上清空,这可能是最可取的结果。即使退出浏览器并将其打开后,LocalStorage仍然存在。
另外,在任何时候,有人想在退出时清空SessionStorage,我使用了:
In order for anyone to find an answer for a similar task, thanks to @Umendra insight, I was able to solve this by using this :
I ended up using sessionStorage over localStorage because it empties itself on window/tab closing which might be the most desirable result. localStorage persists even after exiting the browser and opening it back.
Also, at any point someone wants to empty the sessionStorage on exit, I used :