Well, it works, it just doesn't produce anything worthwhile:
它起作用了,但没有产生任何有价值的东西:
elems = document.getElementById("itemsTable").getElementsByTagName("TR")
for j = 0 to ubound(elems) - 1
' stuff
next
Well, that won't work, apparently elems is an object, not an array like you'd get in that fancy javascript. I'm stuck with vbscript though.
这行不通,显然elems是一个对象,而不是像javascript那样的数组。但是我被vbscript困住了。
So what do I do to iterate all the rows in a table in vbscript?
那么,我要如何遍历vbscript中的表中的所有行呢?
Edit: Yes, it's vbscript and it sucks. I don't have a choice here, so don't say "Use jQuery!!".
编辑:是的,是vbscript,很烂。我在这里没有选择,所以不要说“使用jQuery!!”
3 个解决方案
#1
7
As you have correctly stated getElementsByTagName
does not return an array, hence UBound()
will not work on it. Treat it as a collection.
正如您正确地声明了getElementsByTagName不返回数组,因此UBound()不会对它起作用。把它当作收藏品。
For-Eaching through it should work:
通过它进行操作应该是有效的:
Set NodeList = document.getElementById("itemsTable").getElementsByTagName("TR")
For Each Elem In NodeList
' stuff
MsgBox Elem.innerHTML
Next
#2
1
If you have IE8+, you can use the "item" method. So it'd be:
如果你有IE8+,你可以使用“item”方法。所以它会:
Dim elem: Set elem = document.getElementById("itemsTable").getElementsByTagName("TR").item(1);
#3
0
elems isn't an array in JavaScript either, it is a NodeList, it just happens to share some properties with a JavaScript Array object.
elems也不是JavaScript中的数组,它是NodeList,它只是碰巧与JavaScript数组对象共享一些属性。
I don't know VB, but I assume you could do:
我不知道VB,但我想你可以:
for j = 0 to elems.length - 1
' stuff
next
#1
7
As you have correctly stated getElementsByTagName
does not return an array, hence UBound()
will not work on it. Treat it as a collection.
正如您正确地声明了getElementsByTagName不返回数组,因此UBound()不会对它起作用。把它当作收藏品。
For-Eaching through it should work:
通过它进行操作应该是有效的:
Set NodeList = document.getElementById("itemsTable").getElementsByTagName("TR")
For Each Elem In NodeList
' stuff
MsgBox Elem.innerHTML
Next
#2
1
If you have IE8+, you can use the "item" method. So it'd be:
如果你有IE8+,你可以使用“item”方法。所以它会:
Dim elem: Set elem = document.getElementById("itemsTable").getElementsByTagName("TR").item(1);
#3
0
elems isn't an array in JavaScript either, it is a NodeList, it just happens to share some properties with a JavaScript Array object.
elems也不是JavaScript中的数组,它是NodeList,它只是碰巧与JavaScript数组对象共享一些属性。
I don't know VB, but I assume you could do:
我不知道VB,但我想你可以:
for j = 0 to elems.length - 1
' stuff
next