[英]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. 我曾考虑过使用element
的ID
但是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.