HtmlDocument.GetElementById(String) 方法

定義

利用元素屬性HtmlElement作為搜尋鍵來取得單一ID物件。

public:
 System::Windows::Forms::HtmlElement ^ GetElementById(System::String ^ id);
public System.Windows.Forms.HtmlElement GetElementById(string id);
public System.Windows.Forms.HtmlElement? GetElementById(string id);
member this.GetElementById : string -> System.Windows.Forms.HtmlElement
Public Function GetElementById (id As String) As HtmlElement

參數

id
String

要取回的元素的 ID 屬性。

傳回

回傳與指定值屬性相同的 ID 第一個物件,或 null 若找不到該 id 物件,則回傳。

範例

以下程式碼範例從文件中取得名稱 TABLE ,計算列數,並在網頁中顯示結果。 程式碼範例要求你的專案中有一個WebBrowser名為 WebBrowser1的控制項,且你載入了一個帶有 屬性TABLEIDTable1網頁。

private Int32 GetTableRowCount(string tableID)
{
    Int32 count = 0;

    if (webBrowser1.Document != null)
    {
        HtmlElement tableElem = webBrowser1.Document.GetElementById(tableID);
        if (tableElem != null)
        {
            foreach (HtmlElement rowElem in tableElem.GetElementsByTagName("TR"))
            {
                count++;
            }
        }
        else
        {
            throw(new ArgumentException("No TABLE with an ID of " + tableID + " exists."));
        }
    }

    return(count);
}
Private Function GetTableRowCount(ByVal TableID As String) As Integer
    Dim Count As Integer = 0

    If (WebBrowser1.Document IsNot Nothing) Then

        Dim TableElem As HtmlElement = WebBrowser1.Document.GetElementById(TableID)
        If (TableElem IsNot Nothing) Then
            For Each RowElem As HtmlElement In TableElem.GetElementsByTagName("TR")
                Count = Count + 1
            Next
        Else
            Throw (New ArgumentException("No TABLE with an ID of " & TableID & " exists."))
        End If
    End If
    GetTableRowCount = Count
End Function

備註

如果文件中有多個元素 ID 值相同, GetElementById 則會回傳第一個找到的元素。

適用於

另請參閱