HtmlDocument.GetElementById(String) Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mengambil satu HtmlElement menggunakan atribut elemen ID sebagai kunci pencarian.
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
Parameter
- id
- String
Atribut ID elemen yang akan diambil.
Mengembalikan
Mengembalikan objek pertama dengan atribut yang sama ID dengan nilai yang ditentukan, atau null jika id tidak dapat ditemukan.
Contoh
Contoh kode berikut mengambil bernama TABLE dari dokumen, menghitung jumlah baris, dan menampilkan hasilnya di halaman Web. Contoh kode mengharuskan Anda memiliki WebBrowser kontrol dalam proyek bernama WebBrowser1, dan Anda telah memuat halaman Web dengan TABLE atributnya ID adalah Table1.
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
Keterangan
Jika ada beberapa elemen dalam dokumen dengan nilai ID yang sama, GetElementById akan mengembalikan elemen pertama yang ditemukannya.