从 IE DOM 获取 HTML Head

发布于 2024-12-20 09:30:26 字数 111 浏览 5 评论 0原文

我知道如何使用 IHTMLDocument2 接口获取文档的 HTML 正文(通过调用 get_body 成员函数)。

但是我如何获取头部?IHTMLDocument2 接口中没有这样的函数?

I know how to obtain the HTML body of a document, using the IHTMLDocument2 interface (by calling the get_body member function.

But how can I get the head? There is no such function in the IHTMLDocument2 interface?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一指流沙 2024-12-27 09:30:26
    CComPtr<IHTMLDocument2> htmlDocument;
    CComPtr<IHTMLElementCollection> elementCollection;

    htmlDocument->get_all(&elementCollection);
    for (long i=0;i<numberOfElements;i++)
    {
        _variant_t index = i;
        CComPtr<IHTMLElement> htmlElem;
        CComPtr<IDispatch> htmlElemDisp;
        hResult = elementCollection->item( index,index ,(IDispatch **) &htmlElemDisp );
        if (FAILED(hResult) || (!(htmlElemDisp)))
        {
            continue;
        }
        hResult = htmlElemDisp->QueryInterface( IID_IHTMLElement ,(void **) &htmlElem);
        if (FAILED(hResult) || (!(htmlElem)))
        {
            continue;
        }
        hResult = htmlElem->get_tagName(&buffer);

        if (FAILED(hResult) || (!(buffer)))
        {
            continue;
        }
        if (_wcsicmp(buffer,L"HEAD")==0) 
        { 
         // your code here
        }
}

此外,您还可以使用 IHTMLDocument2* htmlDocument 代替 CComPtrhtml文档
主要思想是获取文档中的所有元素,迭代它们并找到具有 tagName HEAD 的元素。
希望这有帮助。

    CComPtr<IHTMLDocument2> htmlDocument;
    CComPtr<IHTMLElementCollection> elementCollection;

    htmlDocument->get_all(&elementCollection);
    for (long i=0;i<numberOfElements;i++)
    {
        _variant_t index = i;
        CComPtr<IHTMLElement> htmlElem;
        CComPtr<IDispatch> htmlElemDisp;
        hResult = elementCollection->item( index,index ,(IDispatch **) &htmlElemDisp );
        if (FAILED(hResult) || (!(htmlElemDisp)))
        {
            continue;
        }
        hResult = htmlElemDisp->QueryInterface( IID_IHTMLElement ,(void **) &htmlElem);
        if (FAILED(hResult) || (!(htmlElem)))
        {
            continue;
        }
        hResult = htmlElem->get_tagName(&buffer);

        if (FAILED(hResult) || (!(buffer)))
        {
            continue;
        }
        if (_wcsicmp(buffer,L"HEAD")==0) 
        { 
         // your code here
        }
}

Also, you can use IHTMLDocument2* htmlDocument instead of CComPtr<IHTMLDocument2> htmlDocument.
The main idea is to obtain all elements within your document, iterate them and find the one that has the tagName HEAD.
Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文