IHTMLElementCollection::item到底能否返回一个Collection?

gauss 2010-10-17 11:39:51
我的页面里面有两个form,两个form里面有相同名字的radio,如input_1,大致如下,伪代码,大家应能看明白。
<form1>
....
<radio name="input_1" value="a">
<radio name="input_1" value="b">
<radio name="input_1" value="c">
<radio name="input_1" value="d">
...
</form1>

<form2>
...
<radio name="input_1" value="1">
<radio name="input_1" value="2">
<radio name="input_1" value="3">
<radio name="input_1" value="4">
...
</form2>

我的目标:只想找出form1中name叫input_1的radio来。

首先Document的get_ElementsByName是不行了,因为form1和form2都出来了。
我现在已经找到form1,再通过get_all得到全体的Collection,逐个枚举判断,可以找到form1中所有name叫input_1的radio来,但因为form1中element比较多,这样的效率太低。

进而寻求效率更高的办法,似乎只能通过IHTMLElementCollection::item按name进一步筛选。
用IHTMLElementCollection::item返回的IDisp,去进一步QueryInterface IID_IHTMLElementCollection会失败,返回值是0x80070057。尝试过很多次都是这样。

问题的核心,IHTMLElementCollection::item到底能否返回一个Collection?如何返回?
或者有无其他简单的方法高效地实现我的目标?

请大家帮忙给点建议,谢谢!


附:

MSDN中关于IHTMLElementCollection::item的说明

Syntax

Copy
HRESULT item(
VARIANT name,
VARIANT index,
IDispatch **pdisp
);


Parameters

name
[in] VARIANT of type VT_I4 or VT_BSTR that specifies the object or collection to retrieve. If this parameter is an integer, it is the zero-based index of the object. If this parameter is a string, all objects with matching name or id properties are retrieved, and a collection is returned if more than one match is made.index
[in] VARIANT of type VT_I4 that specifies the zero-based index of the object to retrieve when a collection is returned.
pdisp
[out, retval] The address of a pointer that receives an IDispatch interface for the object or collection if successful, or NULL otherwise.
Return Value

Returns S_OK if successful, or an error value otherwise.

Remarks

Note This method indexes collections by the name or id property of the object; this is a known standards-compliance issue. For interoperability with other browsers, do not reference an object by name using this method.
Windows Internet Explorer 8 and later. In IE8 mode, the index parameter is not used. For more information, see Defining Document Compatibility.

Call QueryInterface on the IDispatch interface pointer retrieved to pdisp to obtain the correct interface for the object or collection.
This function returns S_OK even if the element is not found. You should check the value of the pdisp (IDispatch) pointer returned by this call. If the value of the pointer is NULL, the element was not found and the call was not successful.



...全文
718 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lianghaomingg 2011-05-07
  • 打赏
  • 举报
回复
方舟子怎么越来越像流氓?
gauss 2010-12-24
  • 打赏
  • 举报
回复
多谢楼上的,请问您是怎么实现的呢?
zou_j 2010-12-05
  • 打赏
  • 举报
回复
可以的哦
gauss 2010-10-20
  • 打赏
  • 举报
回复
多谢各位提供了一些改进的建议,但问题的核心仍未解决:
IHTMLElementCollection::item到底能否返回一个Collection?

是微软的文档确实错了,还是我们未掌握其中奥妙?
  • 打赏
  • 举报
回复
伪代码:
(1)getElementByTag(“radio”);
(2)遍历每个radio,获取其name和value值
(3)if name=="input_1" && value="a" 则找到你的元素
gauss 2010-10-19
  • 打赏
  • 举报
回复
自己UP一下。
gauss 2010-10-18
  • 打赏
  • 举报
回复

再想想,结合skyxie的1)3)是有简单的办法了。
但是,看看Remark:
Windows Internet Explorer 8 and later. In IE8 mode, the index parameter is not used. For more information, see Defining Document Compatibility.

在IE 8上用不了啊!


[Quote=引用 3 楼 gauss 的回复:]
多谢skyxie,
1)如果按你所说,MSDN的文档是真有错了?你看我已经特别地用红色将返回Coll的说明标起来了。
2)不幸的是我的form1里面有很多组radio,name是input_1, input_2 ... ,用tags过滤好一些,但效率还是低。


引用 2 楼 skyxie 的回复:
1)
这个接口返回的是一个Elem,不是Coll
当name指定的elem有多个时……
[/Quote]
gauss 2010-10-18
  • 打赏
  • 举报
回复

多谢skyxie,
1)如果按你所说,MSDN的文档是真有错了?你看我已经特别地用红色将返回Coll的说明标起来了。
2)不幸的是我的form1里面有很多组radio,name是input_1, input_2 ... ,用tags过滤好一些,但效率还是低。

[Quote=引用 2 楼 skyxie 的回复:]
1)
这个接口返回的是一个Elem,不是Coll
当name指定的elem有多个时,通过index参数确定返回哪一个
[in] VARIANT of type VT_I4 that specifies the zero-based index of the object to retrieve when a collection is returned.
比如有2个name = "abc"……
[/Quote]
skyxie 2010-10-17
  • 打赏
  • 举报
回复
1)
这个接口返回的是一个Elem,不是Coll
当name指定的elem有多个时,通过index参数确定返回哪一个
[in] VARIANT of type VT_I4 that specifies the zero-based index of the object to retrieve when a collection is returned.
比如有2个name = "abc"的elem,则 item("abc", 0, ...) item("abc", 1, ...)分别返回第一二个elem

2)
你种情况 使用 get_children 得到一个coll,在这个coll上 IHTMLElementCollection::tags("radio" 得到所有raido的集合coll2, 然后通过上面的方法遍历coll2即可。

3) 你不清楚name相同的元素个数有多少?
按照上面的方法,index不停++, 当pdisp [out, retval] 为NULL时,就说明遍历完了!
freelaxy 2010-10-17
  • 打赏
  • 举报
回复
IHTMLElement::get_children


=======================================================
FreeLaxy

PS: I am not paied to work here.
=======================================================

3,055

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC HTML/XML
社区管理员
  • HTML/XML社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧