简体   繁体   English

DOM元素属性值可以是JavaScript对象吗?

[英]Can a DOM element attribute value be a JavaScript object?

Maybe this is a duplicate, but I could find the similar question. 也许这是重复的,但我可以找到类似的问题。

For example we have <input data-student=""> and we have a var studentObj = {...} . 例如,我们有<input data-student="">而我们有一个var studentObj = {...} Can I assign studentObj to input data-student attribute? 我可以将studentObj分配给输入data-student属性吗? Will it contradict the standards? 它会与标准相抵触吗?

Attribute values are always strings, so no. 属性值始终是字符串,因此不行。 You have several options: 您有几种选择:

  1. Store the object in string form, such as JSON, in the attribute. 将对象以字符串形式(例如JSON)存储在属性中。

  2. Store the object in a container somewhere and keep a key for it in the attribute. 将对象存储在某个地方的容器中,并在属性中为其保留一个键。

  3. If you mean on an element instance, it's possible to add your own properties to element instances, and those properties can have any value (search for "expando properties" for details), but be sure to use a name that will be really, really specific to your situation. 如果您是指元素实例,则可以将自己的属性添加到元素实例,并且这些属性可以具有任何值(有关详细信息,请搜索“ expando属性”),但请确保使用的名称确实根据您的情况。 (jQuery does this, for example, for the data it manages via the data function.) (例如,jQuery对其通过data功能管理的data执行此操作。)

    • And in fact, if you're using jQuery, you could use data to store the object, leaving the details to jQuery. 实际上,如果您使用的是jQuery,则可以使用data存储对象,而将详细信息留给jQuery。 :-) :-)

Sure will! 一定会的! HTML doesn't accept a data type of object, it is in fact just plain text HTML不接受对象的数据类型,实际上只是纯文本

What you are looking for is the dataset attribute. 您正在寻找的是dataset属性。 But it only stores strings, you can solve this with JSON. 但是它仅存储字符串,您可以使用JSON解决此问题。

el.dataset.student = JSON.stringify(studentObj);

studentObj = JSON.parse(el.dataset.student);

With jquery's data method ( https://api.jquery.com/data/ ) you can store objects without any further problems. 使用jquery的data方法( https://api.jquery.com/data/ ),您可以存储对象而没有任何其他问题。

Read more: 阅读更多:

Dataset: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset - 数据集: https : //developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement/dataset-

JSON: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON JSON: https//developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/JSON

Actually your desire doesn't contradict the standards, you must stringify your object and put in your data attribute then in usage parse it, like below: 实际上,您的需求与标准并不矛盾,您必须对对象进行stringify并放入data属性,然后在使用时对其进行parse ,如下所示:

const q = document.querySelector.bind(document); // it's just for easiness

const input = q('input'); //select the input

const studentObj = { // make your student object
    something: 'some-value'
};

input.dataset.student = JSON.stringify(studentObj);
// put the student object in your selected input data attribute

When you wanna use it you can parse it: 当您想使用它时,可以parse它:

const input = q('input'); //select the input

const myDesireObject = JSON.parse(input.dataset.student);

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

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