存取 Managed HTML 文件物件模型上未公開的成員Accessing Unexposed Members on the Managed HTML Document Object Model

Managed HTML 文件物件模型 (DOM) 包含類別,稱為HtmlElement公開屬性、 方法和所有 HTML 項目都有共通的事件。The managed HTML Document Object Model (DOM) contains a class called HtmlElement that exposes the properties, methods, and events that all HTML elements have in common. 有時候,不過,您將需要存取受管理的介面不會直接公開的成員。Sometimes, however, you will need to access members that the managed interface does not directly expose. 本主題討論兩種方式來存取未公開的成員,包括 Web 網頁內定義的 JScript 和 VBScript 函式。This topic examines two ways for accessing unexposed members, including JScript and VBScript functions defined inside of a Web page.

透過受管理的介面存取未公開的成員Accessing Unexposed Members through Managed Interfaces

HtmlDocumentHtmlElement提供四種方法可讓您存取未公開的成員。HtmlDocument and HtmlElement provide four methods that enable access to unexposed members. 下表顯示的型別以及其對應的方法。The following table shows the types and their corresponding methods.

成員類型Member Type 方法Method(s)
屬性 (HtmlElement)Properties (HtmlElement) GetAttribute

SetAttribute
方法Methods InvokeMember
事件 (HtmlDocument)Events (HtmlDocument) AttachEventHandler

DetachEventHandler
事件 (HtmlElement)Events (HtmlElement) AttachEventHandler

DetachEventHandler
事件 (HtmlWindow)Events (HtmlWindow) AttachEventHandler

DetachEventHandler

當您使用這些方法時,它會假設您有正確的基礎類型的項目。When you use these methods, it is assumed that you have an element of the correct underlying type. 假設您想要聆聽Submit事件的FORMHTML 項目頁面上,以便您可以在執行某些前置處理FORM的值之前的使用者將其提交至伺服器。Suppose that you want to listen to the Submit event of a FORM element on an HTML page, so that you can perform some pre-processing on the FORM's values before the user submits them to the server. 在理想情況下,如果您有 HTML 的控制,您會定義FORM有一個唯一ID屬性。Ideally, if you have control over the HTML, you would define the FORM to have a unique ID attribute.

<HTML>  
  
    <HEAD>  
        <TITLE>Form Page</TITLE>  
    </HEAD>  
  
    <BODY>  
        <FORM ID="form1">  
             ... form fields defined here ...  
        </FORM>  
    </BODY>  
  
</HTML>  

載入到這個頁面之後WebBrowser控制項,您可以使用GetElementById方法來擷取FORM在執行的階段使用form1做為引數。After you load this page into the WebBrowser control, you can use the GetElementById method to retrieve the FORM at run time using form1 as the argument.

private void SubmitForm(String formName)
{
    HtmlElementCollection elems = null;
    HtmlElement elem = null;

    if (webBrowser1.Document != null)
    {
        HtmlDocument doc = webBrowser1.Document;
        elems = doc.All.GetElementsByName(formName);
        if (elems != null && elems.Count > 0) 
        {
            elem = elems[0];
            if (elem.TagName.Equals("FORM"))
            {
                elem.InvokeMember("Submit");
            }
        }
    }
}
Private Sub SubmitForm(ByVal FormName As String)
    Dim Elems As HtmlElementCollection
    Dim Elem As HtmlElement

    If (WebBrowser1.Document IsNot Nothing) Then
        With WebBrowser1.Document
            Elems = .All.GetElementsByName(FormName)
            If (Not Elems Is Nothing And Elems.Count > 0) Then
                Elem = Elems(0)
                If (Elem.TagName.Equals("FORM")) Then
                    Elem.InvokeMember("Submit")
                End If
            End If
        End With
    End If
End Sub

存取未受管理的介面Accessing Unmanaged Interfaces

您也可以使用 DOM 中的每個類別所公開的 unmanaged 的元件物件模型 (COM) 介面來存取 managed HTML DOM 中未公開的成員。You can also access unexposed members on the managed HTML DOM by using the unmanaged Component Object Model (COM) interfaces exposed by each DOM class. 如果您必須呼叫幾個針對未公開的成員,或未公開的成員會傳回其他受管理的 HTML dom。 未包裝的 unmanaged 的介面,這被建議This is recommended if you have to make several calls against unexposed members, or if the unexposed members return other unmanaged interfaces not wrapped by the managed HTML DOM.

下表顯示所有受管理的 HTML dom。 透過公開的 unmanaged 介面The following table shows all of the unmanaged interfaces exposed through the managed HTML DOM. 按一下每個連結,以了解其使用方式及範例程式碼。Click on each link for an explanation of its usage and for example code.

類型Type 未受管理的介面Unmanaged Interface
HtmlDocument DomDocument
HtmlElement DomElement
HtmlWindow DomWindow
HtmlHistory DomHistory

若要使用的 COM 介面的最簡單方式是新增至未受管理的 HTML DOM 文件庫 (MSHTML.dll) 的參考,從您的應用程式,雖然這是不支援。The easiest way to use the COM interfaces is to add a reference to the unmanaged HTML DOM library (MSHTML.dll) from your application, although this is unsupported. 如需詳細資訊,請參閱 < 知識庫文章 934368For more information, see Knowledge Base Article 934368.

存取指令碼函式Accessing Script Functions

HTML 網頁可以定義一或多個函式使用 JScript 或 VBScript 的指令碼語言。An HTML page can define one or more functions by using a scripting language such as JScript or VBScript. 這些函式會放在SCRIPT在頁面中,頁面上,而且可以在 DOM 執行依需求或為了回應事件These functions are placed inside of a SCRIPT page in the page, and can be run on demand or in response to an event on the DOM.

您可以呼叫您定義 HTML 頁面使用的任何指令碼函式InvokeScript方法。You can call any script functions you define in an HTML page using the InvokeScript method. 如果指令碼方法傳回的 HTML 項目,您可以使用轉換來轉換這個傳回的結果,加HtmlElementIf the script method returns an HTML element, you can use a cast to convert this return result to an HtmlElement. 如需詳細資訊和範例程式碼,請參閱InvokeScriptFor details and example code, see InvokeScript.

另請參閱See also