HtmlElement.Parent Właściwość

Definicja

Pobiera element nadrzędny bieżącego elementu.

public:
 property System::Windows::Forms::HtmlElement ^ Parent { System::Windows::Forms::HtmlElement ^ get(); };
public System.Windows.Forms.HtmlElement Parent { get; }
public System.Windows.Forms.HtmlElement? Parent { get; }
member this.Parent : System.Windows.Forms.HtmlElement
Public ReadOnly Property Parent As HtmlElement

Wartość właściwości

Element powyżej bieżącego elementu w hierarchii dokumentu HTML.

Przykłady

Poniższy przykład kodu umożliwia znalezienie wszystkich IMG tagów w dokumencie i użycie Parent właściwości w celu sprawdzenia, czy IMG obiekt jest hiperlinkiem do innej strony. Jeśli tak jest, kod przypisuje adres URL do ALT atrybutu tagu IMG , aby użytkownicy mogli przesunąć obraz, aby zobaczyć, gdzie zostaną one pobrane.

private void AddUrlToTooltip()
{
    if (webBrowser1.Document != null)
    {
        foreach (HtmlElement elem in webBrowser1.Document.GetElementsByTagName("IMG"))
        {
            if (elem.Parent.TagName.Equals("A"))
            {
                String altStr = elem.GetAttribute("ALT");
                if (!(altStr == null) && (altStr.Length != 0))
                {
                    elem.SetAttribute("ALT", altStr + " - points to " + elem.Parent.GetAttribute("HREF"));
                }
                else
                {
                    elem.SetAttribute("ALT", "Points to " + elem.Parent.GetAttribute("HREF"));
                }
            }
        }
    }
}
Private Sub AddUrlToTooltip()
    If (WebBrowser1.Document IsNot Nothing) Then
        With WebBrowser1.Document
            For Each Elem As HtmlElement In .GetElementsByTagName("IMG")
                If (Elem.Parent.TagName.Equals("A")) Then
                    Dim AltStr As String = Elem.GetAttribute("ALT")
                    If (Not (AltStr Is Nothing) And (AltStr.Length <> 0)) Then
                        Elem.SetAttribute("ALT", AltStr & " - points to " & Elem.Parent.GetAttribute("HREF"))
                    Else
                        Elem.SetAttribute("ALT", "Points to " & Elem.Parent.GetAttribute("HREF"))
                    End If
                End If
            Next
        End With
    End If
End Sub

Uwagi

Właściwość Parent umożliwia odnajdywanie kontekstu elementu. Jest to najbardziej przydatne w programach obsługi zdarzeń, takich jak Click, które mogą być uruchamiane dla dowolnego elementu w dowolnym miejscu w hierarchii obiektów dokumentu.

Właściwość Parent elementu HTML (góra dokumentu HTML) wskazuje z powrotem na siebie. Jeśli wywołasz Parent wewnątrz pętli, sprawdź, czy warunek przerwania pętli porównuje typ bieżącego elementu i typ Parent właściwości lub inny kod może wykonać pętlę nieskończoną.

Dotyczy

Zobacz też