MSHTML通过name或者id访问HTML标签

Posted on 2009-08-04 21:21  一有新人  阅读(982)  评论(0编辑  收藏  举报
。。本人菜鸟,大牛请绕道。最近在学习webbrowser控件的时候,需要用到MSHTML来访问页面的标签,并对其进行操作。由于鄙人是新手,于是最先开始GOOGLE,然后MSDN,然后博客园,然后CSDN。。。还是没能找到一个合适的例子来说明如何根据页面标签的id或者name来访问该标签。最终艰辛摸索,结合MSDN上的说明,终于弄出来一个例子。辛苦啊!我将它贴到博客,希望和我一样的新手能够有一个例子,以便较快的熟悉MSHTML的使用,熟悉那套接口的观念(老实说,我觉得这套接口真是搞的麻烦,在JS里就2、3句的代码用它却要几十句才搞得安逸。)

下面是摘自MSDN上关于IHTMLCollection的说明.

IHTMLElementCollection Interface


TProvides access to a collection of element objects.

IHTMLElementCollection Members

_newEnum Retrieves an enumerator for the collection.
item Retrieves an object from the all collection or various other collections.
length Sets or retrieves the number of objects in a collection.
tags Retrieves a collection of objects that have the specified HTML tag name.
toString Retrieves a string representation of the object.

Remarks

If duplicate names are found, a collection of those named items is returned. Collections of duplicate names must be referenced subsequently by ordinal position.

A zero-based collection, in source order, of all elements in a given form can contain any combination of input elements, select elements, and textArea elements.

Interface Information

Stock Implementation mshtml.dll
Custom Implementation No
Inherits from IDispatch
Header and IDL files Mshtml.h, Mshtml.idl
Minimum availability Internet Explorer 4
Minimum operating systems Windows 95, Windows NT 4.0, Windows CE 2.12

使用MSHTML需要包含头文件mshtmlc.h或者mshtml.h
我的示例如下(程序在BCB2009上调试通过):

Code


一点说明:在访问标签时,前提为该标签已经载入。最好是等待页面全部载入再进行操作。否则,可能访问不到标签。
欢迎C++菜鸟加我,一起成长吧!

Copyright © 2024 一有新人
Powered by .NET 8.0 on Kubernetes