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.