可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
So what do I do to iterate all the rows in a table in vbscript?
Edit: Yes, it's vbscript and it sucks. I don't have a choice here, so don't say "Use jQuery!!".
回答1:
As you have correctly stated getElementsByTagName
does not return an array, hence UBound()
will not work on it. Treat it as a collection.
For-Eaching through it should work:
Set NodeList = document.getElementById("itemsTable").getElementsByTagName("TR") For Each Elem In NodeList ' stuff MsgBox Elem.innerHTML Next
回答2:
If you have IE8+, you can use the "item" method. So it'd be:
Dim elem: Set elem = document.getElementById("itemsTable").getElementsByTagName("TR").item(1);
回答3:
elems isn't an array in JavaScript either, it is a NodeList, it just happens to share some properties with a JavaScript Array object.
I don't know VB, but I assume you could do:
for j = 0 to elems.length - 1 ' stuff next