在 JavaScript 的 HTML 标记中存储任意信息?

2023-07-31前端开发问题
0

本文介绍了在 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 标记中存储任意信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

layui tree树组件怎么自定义添加图标
经常用到layui的朋友都知道,layui tree默认是不能自定义图标的,那么我们要自定义的话要怎么操作呢? 首先用编辑器软件(修改时候用编辑器记得编码),打开layui.js。搜索: i class="layui-icon layui-icon-file" 改为如下代码: i class="'+ (i.icon || "l...
2024-10-26 前端开发问题
493

layui要如何改变时间日历布局大小?
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24 前端开发问题
271

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125