在 JavaScript 的 HTML 标记中存储任意信息? [英] Storing arbitrary info in HTML tags for JavaScript?

查看:16
本文介绍了在 JavaScript 的 HTML 标记中存储任意信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个非常笼统的问题.

我正在动态生成一个表单,该表单使用 HTML/JavaScript 拆分为多个级别的选项卡.

I am dynamically generating a form that is split into multiple levels of tabs using HTML / JavaScript.

我想使用 CSS 和 background-image 用星 * 符号突出显示一些字段(值与全局模板不同的字段).

I want to highlight some of the fields (those that have a value differing from a global template) with a star * symbol using CSS and background-image.

JS 字段遍历每个字段,比较其值,并在必要时为其设置 CSS 类.到目前为止,一切顺利.

A JS field iterates through each field, compares its value, and sets a CSS class for it if necessary. So far, so good.

现在,我不仅希望每个更改的字段都用星号标记,而且还希望它所在的选项卡.

Now in addition, I want not only every changed field to be marked with a star, but also the tab it is in.

因为我喜欢它简单,所以我想将选项卡的元素 ID 作为属性存储在每个字段的 HTML 标记中的某处(类似于parentTab").然后,JS 函数会突出显示该字段及其parentTab"元素(也可能是那个parentTab"元素).

Because I love it simple, I would like to store the element ID of the tab as an attribute somewhere in each field's HTML tag (something like "parentTab"). The JS function then highlights the field and its "parentTab" element (and maybe that one's "parentTab" as well).

我的第一种方法是滥用title"属性或其他东西来存储 parentTab.当然,这很脏.但是,如果我只是在 DIV 或 INPUT 标记中随意添加任意属性,它将不再有效,并且我觉得访问这些属性不太安全 - 谁知道不同的浏览器如何处理它,并且将来会处理它?

My first approach is to misuse the "title" attribute or somethiong to store parentTab in. Of course, that's dirty. However, if I just wildly add arbitrary attributes to the DIV or INPUT tag, it won't validate any more and I feel less than secure accessing these attributes - who knows how different browsers handle it, and will handle it in the future?

所以我的问题是:是否有一种有效的、符合标准的方式——某种属性——将任意数据存储在 HTML 标记中,以供 JavaScript 进一步处理?

So my question is: Is there a valid, standards compliant way - an attribute of some sort - to store arbitrary data inside HTML tags, for further processing by JavaScript?

当然,我可以创建一个parentTabs"JS 数组并完成它.但是将其存储在输入本身中会更加优雅.

Of course, I could create a "parentTabs" JS array and be done with it. But storing it in the input itself would be so much more elegant.

推荐答案

您可以通过编写 isChildOf 函数来找出字段属于哪个选项卡式元素,如下所示:http://jimkeller.blogspot.com/2008/07/jquery-ischildof-is-element-child-of.html

You could find out which tabbed element a field belongs to by writing an isChildOf function, like this: http://jimkeller.blogspot.com/2008/07/jquery-ischildof-is-element-child-of.html

使用 DOM 来解决这个问题总是比以某种自定义格式复制数据更优雅".

Using the DOM to work this out will always be more "elegant" than duplicating the data in some custom format.

这篇关于在 JavaScript 的 HTML 标记中存储任意信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆