如题
其实我是想建立完整的网页元素树,但是无法将 frame 和元素节点对应起来,父子结构无法体现,怎么办?

解决方案 »

  1.   

    你把每个网页的标签设置个ID,然后JS不是有个:document.getElementById('').xxx//根据这个来取得,很久以前学的JS,记不怎么清楚了。
      

  2.   

    打个比方说明一下,你现在的一个div或者其它elem是如何建立关系的?然后别人才有可能帮你跟着你的思路去找。TWebBrowser打开html后,里面其实就有这些关系。
      

  3.   


    procedure TFMWebBrowser.RefreshHTMLElementTree;
    function
      _AddElementNode(AParent: TcxTreeListNode; AItem: IHTMLElement): TcxTreeListNode;
      var
        lCaption: string;
        lDataElement: TDataElement;
      begin
        lCaption := '';
        if Trim(AItem.className) <> '' then lCaption := ' class=' + AItem.className;
        if lCaption = '' then lCaption := VarToStr(AItem.getAttribute('class', 0));
        if Trim(AItem.id) <> '' then lCaption := ' id=' + AItem.id;
        lCaption := '<' + AItem.tagName + lCaption + '>';
        Result := AParent.AddChild;
        Result.Values[0] := lCaption;
        lDataElement := TDataElement.Create;
        lDataElement.PElement := AItem;
        Result.Data := lDataElement;
      end;  
    procedure
      _FullChildItem(AParent: TcxTreeListNode; AItem: IHTMLElement);
      var
        M, N: Integer;
        lChilds: IHTMLElementCollection;
        lItem: IHTMLElement;
        lThisFrame: IHTMLWindow2;
        lFrame: IDispatch;
        lItemNode: TcxTreeListNode;
        lurl, lsrc: string;
      begin
        lChilds := AItem.children as IHTMLElementCollection;
        for M := 0 to lChilds.Length - 1 do
        begin
          lItem := lChilds.Item(M, varEmpty) as IHTMLElement;
          lItemNode := _AddElementNode(AParent, lItem);
          if (lItem.tagName = 'IFRAME') or (lItem.tagName = 'FRAME') then
          begin
            lThisFrame := nil; //下面的代码也无法使 lItem 和 IHTMLWindow4 联系起来
            for N := 0 to (lItem.document as IHTMLDocument2).frames.length - 1 do
            begin
              lFrame := (lItem.document as IHTMLDocument2).frames.item(N);
              lurl := (lFrame as IHTMLWindow4).frameElement.src;
              lsrc := lItem.getAttribute('src', 0);
              if CompareText(RightStr(lurl, Length(lsrc)), lsrc) = 0 then //比较src有时候不行,如果比较name有时候会重复
              begin
                lThisFrame := lFrame as IHTMLWindow2;
                Break;
              end;
            end;
            if lThisFrame <> nil then
            begin
              lItemNode := _AddElementNode(lItemNode, lThisFrame.Document.body);
              _FullChildItem(lItemNode, lThisFrame.Document.body);
            end;
          end else
          begin
            if (lItem.children as IHTMLElementCollection).Length <> 0 then
              _FullChildItem(lItemNode, lItem);
          end;
        end;
      end;
    var
      lItemNode: TcxTreeListNode;
    begin
      ctlElementTree.Clear; 
      lItemNode := _AddElementNode(ctlElementTree.Root, (WebBrowser.Document as IHTMLDocument2).body);
      _FullChildItem(lItemNode, (WebBrowser.Document as IHTMLDocument2).body);
    end;
      

  4.   

    什么情况不行?能否提取出来个例子html,1个可以的,1个不行的?