【发布时间】:2019-04-14 01:18:09
【问题描述】:
有没有一种方法可以创建多个 JS 对象,每个都与具有唯一 ID 的单独 HTML 元素相关?
还有一种方法可以在没有 ID 的情况下执行此操作,例如使用 class[1]?
我似乎找不到任何答案
HTML
<div class="exampleClass" id="example1"> </div>
<div class="exampleClass" id="example2"> </div>
<div class="exampleClass" id="example3"> </div>
<div class="exampleClass" id="example4"> </div>
JS
var Object1 = {
data1: "This goes with the first DIV"
}
var Object2 = {
data1: "This goes with the second DIV"
}
var Object3 = {
data1: "This goes with the third DIV"
}
var Object4 = {
data1: "This goes with the fourth DIV"
}
我想将每个 JS 对象关联到它自己的 ID/元素
【问题讨论】:
-
你试过数组吗?
-
你能进一步解释一下吗?
-
const arr = { data1: "This goes with the first DIV" }, ...然后是元素的集合,第0个元素将对应arr[0]
标签: javascript jquery html arrays object