简体   繁体   English

保存HTML元素的数据,以便以后可以访问

[英]save data for an HTML element such that it can be accessed later

Everything I've read says do not save custom properties or attributes to HTML DOM Elements. 我读过的所有内容都不会将自定义属性或属性保存到HTML DOM Elements中。 So I'm trying to figure out how else I should save properties/attributes for an element such that I can access them later. 因此,我试图弄清楚我还应该如何保存元素的属性/属性,以便以后可以访问它们。

Originally I was thinking of using the element as the key in a hash but JS converts hash keys to string so that won't work. 最初,我想将元素用作哈希中的键,但是JS会将哈希键转换为字符串,因此无法正常工作。

Use case: 用例:

function do1(element)
{
    var w = element.style.width;
    element.style.width = "200px";

    // i want to save the w variable for this element somewhere/somehow
}

function do2(element)
{
    // i want to be able to get the w variable i saved earlier for the element
}

I thought of using the element 's ID but the element won't always have an ID that I can use and I can't set one because there might be other JS that dynamically sets ID s for elements. 我曾考虑过使用elementID但是element不会总是有我可以使用的ID ,我无法设置一个ID ,因为可能还有其他JS为元素动态设置ID

Why not use data attributes ? 为什么不使用数据属性 They're specifically intended for storing extra data on an element. 它们专门用于在元素上存储额外的数据。

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

相关问题 以后可以访问添加到 javascript 数组中的数据吗? - Can data added in javascript arrays be accessed later? 如何访问此脚本元素的数据? - How can the data of this script element be accessed? 我可以保存/导出html页面输入数据并在以后恢复/加载吗? - Can I save/export html page input data and restore/load it later? 将HTML解析为DOM,以便稍后由getElementById访问 - Parsing HTML into DOM to be later accessed by getElementById Java稍后将在哪里存储数据? - Where to store data which will be later accessed by Javascript? 将画布绘图另存为数据或图像,以便以后插入html页面 - save canvas drawing as data or image for later insertion into html page 如何在ASP脚本中设置.data()变量,以便以后可以访问? - How to set .data() variable inside an ASP script so it can be accessed later? 如何存储现有的 html 元素并在以后创建它? - How can I store an existing html element and create it later? 如何将数据保存在 session 存储中并稍后获取? - How can I save data in my session storage and get it later? 有没有办法制作一个 function 可以稍后在谷歌浏览器扩展中访问? - Is there a way to make a function that can be accessed later in a google chrome extension?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM