0

说 dw1 是一个类型为 msthml.htmldocument 的变量

dw1.all 类型为 mshtml.ihtmlelementcollection

但是, dw1.body.all 类型是对象。

为什么会这样?

说得更直白点。

为什么 type ofdw1.all与type of 不同dw1.body.all

4

1 回答 1

1

我得到以下

在此处输入图像描述

当 DOM 加载并解析时,您将获得 body.All 是当前元素下所有元素的 HtmlElementCollection,如https://msdn.microsoft.com/en-us/library/system.windows.forms 中所定义。 htmlelement.all(v=vs.110).aspx

此表将帮助浏览此结构 https://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument(v=vs.110).aspx

All - 获取 HtmlElementCollection的实例,该实例存储文档的所有 HtmlElement 对象。

Body - 获取 BODY 标记的 HtmlElement  。

这是加载 DOM 的方式

        // Construct DOM
        HTMLDocument doc = new HTMLDocument();

        // Obtain the document interface
        IHTMLDocument2 htmlDocument = (IHTMLDocument2)doc;
        string htmlContent = "<!DOCTYPE html><html><body><h2>An unordered HTML list</h2><ul>  <li>Coffee</li>  <li>Tea</li>  <li>Milk</li></ul></body></html>";

        // Load the DOM
        htmlDocument.write(htmlContent);

        // Extract all body elements
        IHTMLElementCollection allBody = htmlDocument.body.all;


        // All page elements including body, head, style, etc 
        IHTMLElementCollection all = htmlDocument.all;

        // Iterate all the elements and display tag names
        foreach (IHTMLElement element in allBody)
        {
            Console.WriteLine(element.tagName);
        }
于 2016-07-27T18:13:55.837 回答