简体   繁体   中英

iterate over IHTMLElementCollection

is there a way to iterate over IHTMLElementCollection?

such as

var
  e : IHTMLLinkElement;
elementCollection:IHTMLElementCollection;
begin
    for e in elementCollection do
      showmessage(e.caption);
end;

i know there is a property named _newEnum, however it is not supported in delphi as much as i could understand.

update: apperently links are IHTMLElement and not IHTMLLinkElement

for I := 0 to Pred(elementCollection.length) do
begin
  e := elementCollection.item(I, EmptyParam) as IHTMLElement;
  //...
end;

The code to use _newEnum looks like this. If you're sure you've only got link elements items in the collection, you could change the as IHTMLElement part (and the elem value type) of the inner loop to as IHTMLAnchorElement (IHTMLLinkElement appears to be something entirely different)

uses MSHTML, ActiveX;

var
  collection:IHTMLElementCollection;
  enum:IEnumVariant;
  v:OleVariant;
  u:IUnknown;
  element:IHTMLElement;
begin
  //...
  enum:=collection._newEnum as IEnumVariant;
  while enum.Next(1,v,u)=S_OK do
   begin
    elem:=u as IHTMLElement;
    //...
   end;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM