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

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

问题描述



我使用HTML / JavaScript动态生成一个分为多个标签级别的表单。



我希望使用CSS和背景图像来突出显示一些字段(与全局模板不同的值),并使用星号*符号。



JS字段遍历每个字段,比较其值,并在必要时为其设置CSS类。到目前为止,这么好。



现在,我不仅希望每个更改的字段都标有明星,还要标记标签它是在。



因为我喜欢它简单,我想将标签的元素ID作为属性存储在每个字段的HTML标签中(类似于parentTab )。 JS函数然后突出显示该字段及其parentTab元素(也可能是该元素的parentTab)。

我的第一种方法是滥用title属性或somethiong来存储parentTab。当然,这很脏。但是,如果我只是疯狂地将任意属性添加到DIV或INPUT标记中,它将不再进行验证,我感觉不到安全地访问这些属性 - 谁知道不同的浏览器如何处理它,并且将来会处理它? / p>

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

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

解决方案

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



使用DOM工作这样做总比用一些自定义格式复制数据更优雅。


A very general question.

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

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.

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.

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).

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?

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?

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.

解决方案

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

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天全站免登陆