HtmlDocument.GetElementById(String) メソッド

定義

要素の ID 属性を検索キーとして使用して、1 つの HtmlElement を取得します。

public:
 System::Windows::Forms::HtmlElement ^ GetElementById(System::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 属性。

戻り値

HtmlElement

指定した値と同じ ID 属性を持つ最初のオブジェクト、または null が見つからない場合は id を返します。

次のコード例では、ドキュメントから名前付きを TABLE 取得し、行数をカウントして、結果を Web ページに表示します。 このコード例では、プロジェクトに名前がWebBrowser付いたWebBrowser1コントロールがあり、属性Table1が .1 の Web ページをTABLE``ID読み込んでいる必要があります。

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 最初に見つかる要素が返されます。

適用対象

こちらもご覧ください