简体   繁体   English

我们可以在没有jQuery的情况下将对象分配给纯JavaScript中的html元素吗

[英]Can we assign an object to a html element in plain JavaScript without jQuery

I want to store an object as an attribute of an HTML element! 我想将对象存储为HTML元素的属性! Let's suppose I'm having a div element and when I click the div element I want to get the values of the object from the div element so that I can use it in the click function. 假设我有一个div元素,当我单击div元素时,我想从div元素获取对象的值,以便可以在click函数中使用它。

I've seen people doing it in jquery but I want it in pure javascript since I'm writing a typescript code and I don't want jQuery 我见过有人在jquery中这样做,但是我想用纯JavaScript编写,因为我正在编写打字稿代码,而且我不想要jQuery

ex: 
var myNewObject={
"first_name": "Sam",
"last_name": "carter"
}
var div=document.getElementByID("myID");
div.setAttribute("myobject",myNewObject);

<div onclick="somefunc()>
</div>
function somefunc()
{
     console.log(document.getAttribute("myobject").first_name);

}

Expected output: Sam 预期输出:Sam
Actual output: Error 实际输出:错误

You can store any Javascript objects on an HTMLElement directly: 您可以将任何Javascript对象直接存储在HTMLElement

const div = document.createElement('div');
div.myobject = { hello: 'world' };

Only strings can be stored in attributes, and the string representation of an object is [object Object] . 属性中只能存储字符串,并且对象的字符串表示形式为[object Object] If you must use attributes you can serialize the object and store it and then deserialize it when you retrieve it again: 如果必须使用属性,则可以序列化对象并存储它,然后在再次检索它时反序列化它:

 const obj = { hello: 'world' }; const a = document.querySelector('#a'); a.setAttribute('myobject', JSON.stringify(obj)); const obj2 = JSON.parse(a.getAttribute('myobject')); console.log(obj2.hello); 
 <div id="a">Hello</div> 

Storing state in DOM is generally considered bad practice in any case. 在任何情况下,将状态存储在DOM中通常被认为是不好的做法。

Here is another approach. 这是另一种方法。 This is a good time to learn about Map which is similar to an object but the key can be any type, not just strings. 这是学习与对象相似的Map的好时机,但是键可以是任何类型,而不仅仅是字符串。 You can use the element as a key and the object as the value. 您可以将元素用作键,将对象用作值。

const elementMap = new Map();
const myElement = document.querySelector("#myElement");
const myObject = {x: 0, y: 0, z: 0};
elementMap.set(myElement, myObject);
...
//later we can access the object by that element
const myElement = document.querySelector("#myElement");
const myObject = elementMap.get(myElement);

Here are two big advantages of this approach. 这是此方法的两个主要优点。
1. No need to worry about name collisions with properties of the HTML element, or possibly other code modifying the element's properties. 1.无需担心与HTML元素的属性发生名称冲突,也不必担心修改该元素的属性的其他代码。
2. The object is the same object you originally had, which can be important if you care about the reference which is not preserved if you use the JSON string solution. 2.该对象与您最初拥有的对象相同,如果您关心使用JSON字符串解决方案时不保留的引用,则这很重要。

暂无
暂无

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

相关问题 无 html 标记的 html 元素的普通 javascript 选择器 - Plain javascript selector for html element without html markup 我们可以在javascript中将对象分配到cookie中吗? - can we assign object into cookie in javascript? 一种在不使用jquery的情况下使用另一个html中的object标签显示网页的更好方法,仅使用纯JavaScript - A better way to display a webpage using object tag in another html without using jquery, just plain javascript 将 javascript 对象分配给 html 元素的最佳方法 - Best way to assign a javascript object to html element 如何在不使用任何mysql查询,仅使用简单的javascript或jquery的情况下在HTML表中进行搜索? - How can I search in a HTML table without using any mysql queries, just a plain javascript or jquery to be specific? 在jquery $({})中包装普通的javascript对象 - wrapping plain javascript object in jquery $({}) 我们可以在对象构造函数内分配一个公共方法吗? (JavaScript的) - Can we assign a public method inside a object constructor ? (javascript) 如何在不知道JavaScript中的id / class / tag的情况下分配给HTML元素? - How to assign to an HTML element without knowing id/class/tag in javascript? 在不更改html元素的情况下处理jQuery对象 - Manipulating jQuery object without changing the html element JavaScript在不知道级别的情况下将值分配给嵌套对象中的元素 - JavaScript assign value to element in nested object without knowing level
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM