Html document.getElementsByTagName 在 vbscript 中工作吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1500921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
does document.getElementsByTagName work in vbscript?
提问by jcollum
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!!”。
回答by Yannick Motton
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:
For-Eaching 通过它应该可以工作:
Set NodeList = document.getElementById("itemsTable").getElementsByTagName("TR")
For Each Elem In NodeList
' stuff
MsgBox Elem.innerHTML
Next
回答by Eugene
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);
回答by Quentin
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 Array 对象共享一些属性。
I don't know VB, but I assume you could do:
我不知道 VB,但我认为你可以这样做:
for j = 0 to elems.length - 1
' stuff
next