简体   繁体   English

将DOM元素保存在SessionStorage中

[英]Save DOM Element in SessionStorage

The idea is pretty simple, yet I cannot figure it out. 这个想法很简单,但我无法弄清楚。 Basically I want to save an element that was clicked in the sessionStorage. 基本上,我想保存一个在sessionStorage中单击的元素。 However, this is not easy because sessionStorage only takes strings. 但是,这并不容易,因为sessionStorage只接受字符串。

I am attatching a click event to all my navigation buttons. 我正在为所有导航按钮添加一个click事件。 (This does not work of course) (这当然不起作用)

navButtons.addEventListener("click", function () {
    sessionStorage['active-nav'] = $(this);
});

How would I save a DOM element and access it later? 如何保存DOM元素并在以后访问?

EDIT 编辑

Basically when the page is reloaded, I want to keep track which element was clicked before and the apply some CSS to it once the new page is loaded. 基本上,当重新加载页面时,我想跟踪之前单击过哪个元素,并在加载新页面后对其应用一些CSS。 For example changing the background color of that element to orange, etc. Therefore, I need to somehow save some information about that element in the sessionStorage and access the element/ find the element, once the page is reloaded. 例如,将该元素的背景颜色更改为橙​​色等。因此,一旦重新加载页面,我需要以某种方式在sessionStorage中保存有关该元素的一些信息并访问该元素/查找该元素。 I used the approach of adding an Id to all elements and then using the javascript function document.getElementById("test") , to find it. 我使用了向所有元素添加ID的方法,然后使用javascript函数document.getElementById("test")进行查找。 However, there are quite a few navigation elements, so adding Ids to all of them would probably not be the cleanest solution. 但是,导航元素很多,因此将ID添加到所有导航元素可能不是最干净的解决方案。 What would be the best way of solving this wihtout adding Ids to everything? 解决此问题的最佳方法是什么?

You can set the sessionStorage value to .outerHTML of the element. 您可以将sessionStorage值设置为元素的.outerHTML

sessionStorage['active-nav'] = this.outerHTML;

If more data than only the HTML is needed you can store a JSON string at sessionStorage , where one of the values is the .outerHTML . 如果仅需要HTML以外的数据,则可以在sessionStorage存储JSON字符串,其中一个值是.outerHTML

navButtons.addEventListener("click", function () {
   sessionStorage.setItem('div', this.outerHTML);
});

Save using the outerHTML property of the element 使用元素的outerHTML属性进行保存

To get the same element 获得相同的元素

var element=sessionStorage.getItem('div');

I'd say you should either: 我想你应该要么:

  • Store it's id , then use document.querySelector() to reselect it using the stored id. 存储它的id ,然后使用document.querySelector()通过存储的ID重新选择它。

  • Produce & store a reverse selector , by using a library such as SimmerJS , then use document.querySelector() to reselect it using the stored selector. 通过使用诸如SimmerJS之类的库来生成并存储反向选择器 ,然后使用document.querySelector()使用存储的选择器重新选择它。

Reverse selectors avoid the need to add explicit ids to all relevant elements. 反向选择器避免了向所有相关元素添加显式ID的需要。

Here's how reverse selectors work: 反向选择器的工作方式如下:

 window.simmer = window.Simmer.configure({ depth: 10 }) document .querySelector('.container') .addEventListener('click', e => { console.log(simmer(e.target)) }) 
 <script src="https://cdn.jsdelivr.net/npm/simmerjs@0.5.6/dist/simmer.js"></script> <div class="container"> <div class="left"> <button> Click Me </button> </div> <div class="right"> <button> Click Me </button> </div> </div> 

Then save the produced selector and use it again to reselect the element on page load. 然后保存产生的选择器,并再次使用它在页面加载时重新选择元素。 No explicit ids needed. 无需明确的ID。

Saving selectors avoids serialising & rehydrating HTML, so you don't loose any event listeners attached to the element, plus they won't take up a lot of storage space in case you plan to do this for a lot of elements. 保存选择器可以避免对HTML进行序列化和重新水化,因此您不会丢失附加到该元素的任何事件侦听器,而且它们不会占用大量存储空间,以防您打算对许多元素执行此操作。

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

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