does document.getElementsByTagName work in vbscript?

匿名 (未验证) 提交于 2019-12-03 01:20:02

问题:

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 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!