HtmlElement.GetAttribute(String) Metoda
Definicja
Ważny
Niektóre informacje dotyczą produktów przedpremierowych, które mogą zostać znacznie zmodyfikowane przed premierą. Microsoft nie udziela żadnych gwarancji, ani wyraźnych, ani domniemanych, dotyczących informacji podanych tutaj.
Pobiera wartość nazwanego atrybutu w elemecie .
public:
System::String ^ GetAttribute(System::String ^ attributeName);
public string GetAttribute(string attributeName);
member this.GetAttribute : string -> string
Public Function GetAttribute (attributeName As String) As String
Parametry
- attributeName
- String
Nazwa atrybutu. Ten argument jest bez uwzględniania wielkości liter.
Zwraca
Wartość tego atrybutu w elemecie String jako wartość. Jeśli określony atrybut nie istnieje w tym elemecie, zwraca pusty ciąg.
Przykłady
Poniższy przykład kodu pobiera wszystkie META tagi w dokumencie HTML, używając polecenia GetAttribute , aby znaleźć META tag o nazwie Description. Przykład wymaga, aby aplikacja ma kontrolkę WebBrowser o nazwie WebBrowser1.
private void DisplayMetaDescription()
{
if (webBrowser1.Document != null)
{
HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("META");
foreach (HtmlElement elem in elems)
{
String nameStr = elem.GetAttribute("name");
if (nameStr != null && nameStr.Length != 0)
{
String contentStr = elem.GetAttribute("content");
MessageBox.Show("Document: " + webBrowser1.Url.ToString() + "\nDescription: " + contentStr);
}
}
}
}
Private Sub DisplayMetaDescription()
If (WebBrowser1.Document IsNot Nothing) Then
Dim Elems As HtmlElementCollection
Dim WebOC As WebBrowser = WebBrowser1
Elems = WebOC.Document.GetElementsByTagName("META")
For Each elem As HtmlElement In Elems
Dim NameStr As String = elem.GetAttribute("name")
If ((NameStr IsNot Nothing) And (NameStr.Length <> 0)) Then
If NameStr.ToLower().Equals("description") Then
Dim ContentStr As String = elem.GetAttribute("content")
MessageBox.Show("Document: " & WebOC.Url.ToString() & vbCrLf & "Description: " & ContentStr)
End If
End If
Next
End If
End Sub
Uwagi
Atrybut w kodzie HTML jest dowolną prawidłową parą nazw/wartości dla tego elementu.
HtmlElement uwidacznia tylko te atrybuty, które są wspólne dla wszystkich elementów, pomijając te, które mają zastosowanie tylko do niektórych typów elementów; SRC jest wstępnie zdefiniowanym atrybutem tagu IMG , na przykład, ale nie dla tagu DIV . Użyj GetAttribute atrybutów SetAttribute , które nie są uwidocznione w zarządzanym modelu obiektów dokumentów (DOM, Document Object Model).
GetAttribute i SetAttribute są bez uwzględniania wielkości liter.