经过研究我发现要在 HTA 中动态更新表,我需要添加 tbody
元素。我还可以看到,然后我需要使用 appendchild
函数将必要的数据/行添加到表中。
我已经完成了这个并且正在尝试使用下面的代码遍历数组 ArrLogs
Dim i
i = 1
Set table = document.getElementById("maintable")
Set tbody = document.createElement("tbody")
table.appendChild(tbody)
Set trow = document.createElement("tr")
Set tcol = document.createElement("td")
ArrLogs = ReadLogs(computerasset.value)
Do Until i = UBound(ArrLogs)
tcol.innerHTML = ArrLogs(i)
trow.appendChild(tcol)
tbody.appendChild(trow)
table.appendChild(tbody)
i = i+1
Loop
我遇到的问题是我只看到我的数组的最后一个值附加到表中,就好像我缺少保存附加的命令并且它在运行时覆盖行?
我很清楚这不整洁,或者循环遍历数组的正确方法(应该使用 for i = 1 to UBound(ArrLogs)
等)——我正在测试不同的在我犯了明显错误的情况下做事的方法。
最佳答案
trow.appendChild(tcol)
不会复制 tcol
到行;它插入一个 reference 到它,这意味着你只有 one tcol
你经常覆盖,例如下面的代码将显示 B 而不是 A
Set p = document.createElement("p")
p.innerHTML = "A"
document.body.appendChild(p)
p.innerHTML = "B"
要解决此问题,请在循环中创建新元素:
Dim i: i = 0
Set tbody = document.createElement("tbody")
ArrLogs = ReadLogs(computerasset.value)
for i = lbound(ArrLogs) to ubound(ArrLogs)
Set trow = document.createElement("tr")
Set tcol = document.createElement("td")
tcol.innerHTML = ArrLogs(i)
trow.appendChild(tcol)
tbody.appendChild(trow)
Next
document.getElementById("maintable").appendChild(tbody)
关于html - 在 HTA (VBS) 中动态更新表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33752695/