HtmlDocument.GetElementById(String) 方法    
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使用 元素的 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.HtmlElementPublic Function GetElementById (id As String) As HtmlElement参数
- id
- String
要检索的元素的 ID 特性。
返回
如果找不到 ,则返回具有与指定值nullid相同的ID属性的第一个对象。
示例
下面的代码示例从文档中检索名为 TABLE 的 ,计算行数,并在网页中显示结果。 代码示例要求在项目中具有名为 WebBrowser 的 WebBrowser1控件,并且已加载具有 其属性为 Table1的 ID 的网页TABLE。
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 将返回它找到的第一个元素。