简体   繁体   English

如何在其他页面上的元素上添加类名称

[英]How to add a class name to an element on a different page

I have an image slider where the class name 'current' changes when the next and previous buttons are clicked to display the current image. 我有一个图像滑块,单击下一个和上一个按钮以显示当前图像时,类名“当前”会更改。 I wanted to create a separate Overview page which displays all the images in a grid, then when an image is clicked you are directed to the Slider page, and the selected image has the class of 'current'. 我想创建一个单独的“概述”页面,该页面在网格中显示所有图像,然后单击图像时将您定向到“滑块”页面,并且所选图像的类别为“当前”。 Because I can't access and add/remove the 'current' class from my Overview page, I'm not sure how to achieve the functionality I'm looking for. 因为无法从“概述”页面访问和添加/删除“当前”类,所以我不确定如何实现所需的功能。 I'm hoping there's a way to load the Slider page and pass in a function which alters the class names on the page you're directed to. 我希望有一种方法可以加载Slider页面并传递一个函数,该函数可以更改您所指向的页面上的类名。

Any help or suggestions on a different way to approach this would be appreciated. 以其他方式解决此问题的任何帮助或建议,将不胜感激。

Also, this is just using Vanilla JS. 另外,这只是使用Vanilla JS。 I'm relatively new and still trying to understand the basics. 我是新手,但仍在尝试了解基本知识。

Thanks! 谢谢!

The easiest solution that comes to mind is using GET parameter : https://example.com/overview?currentimage=1 . 我想到的最简单的解决方案是使用GET参数https://example.com/overview?currentimage=1 : https://example.com/overview?currentimage=1 This way you include the image id in the url and can act accordingly on the overview page. 这样,您可以在网址中包含图片ID,并可以在概述页面上采取相应的措施。 This however works only as you open the new page. 但是,这仅在您打开新页面时起作用。

If you'd like the image to change in real time on the overview page as it changes on the slider, you have to look into cookies , session , and/or HTML5 Web Storage . 如果您希望图像随着滑块上的更改而在概览页面上实时更改,则必须查看cookiessession和/或HTML5 Web Storage These allow you to store and share data across multiple pages. 这些使您可以跨多个页面存储和共享数据。 You update the cookie/session/storage on slider change, and then retrieve the data on the overview page with a loop that checks for changes every second or two, and updates the grid. 您可以在滑块更改时更新Cookie /会话/存储,然后使用循环检索概述页面上的数据,该循环每隔一两秒钟检查一次更改,并更新网格。

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

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