HtmlDocument.GetElementById(String) 方法

定義

使用 專案的 屬性作為搜尋索引鍵, ID 擷取單 HtmlElement 一 。

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 屬性的第一個物件,如果找不到 ,則 nullid 回 。

範例

下列程式碼範例會從檔擷取具名 TABLE 的 、計算資料列數目,並在網頁中顯示結果。 程式碼範例會要求您在名為 的專案中具有 WebBrowser 控制項,而且您已載入具有 其 ID 屬性為 Table1 的網頁 TABLEWebBrowser1

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

備註

如果檔中有多個元素具有相同的識別碼值, GetElementById 則會傳回它找到的第一個專案。

適用於

另請參閱