Редагувати

Поділитися через


HtmlElement.Parent Property

Definition

Gets the current element's parent element.

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

Property Value

The element above the current element in the HTML document's hierarchy.

Examples

The following code example finds all of the IMG tags in a document, and uses the Parent property to test whether the IMG is hyperlinked to another page; if it is, the code assigns the URL to the ALT attribute of the IMG tag, so that users can mouse over the image to see where it will take them.

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

Remarks

The Parent property enables discovery of an element's context. It is most useful inside of event handlers such as Click, which can fire for any element anywhere in the document's object hierarchy.

The Parent property of the HTML element (the top of an HTML document) points back to itself. If you call Parent inside a loop, verify that the loop's break condition compares the type of the current element and the type of the Parent property, or else your code may execute an infinite loop.

Applies to

See also