Condividi tramite


HtmlElement.Parent Proprietà

Definizione

Ottiene l'elemento padre dell'elemento corrente.

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

Valore della proprietà

Elemento sopra l'elemento corrente nella gerarchia del documento HTML.

Esempio

Nell'esempio di IMG codice seguente vengono trovati tutti i tag in un documento e viene utilizzata la Parent proprietà per verificare se IMG il collegamento ipertestuale è a un'altra pagina. In caso affermativo, il codice assegna l'URL all'attributo ALT del IMG tag, in modo che gli utenti possano passare il mouse sull'immagine per vedere dove verranno usati.

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

Commenti

La Parent proprietà abilita l'individuazione del contesto di un elemento. È più utile all'interno dei gestori eventi, ad Clickesempio , che può essere generato per qualsiasi elemento in qualsiasi punto della gerarchia degli oggetti del documento.

La Parent proprietà dell'elemento HTML (la parte superiore di un documento HTML) punta a se stessa. Se si chiama Parent all'interno di un ciclo, verificare che la condizione di interruzione del ciclo confronti il tipo dell'elemento corrente e il tipo della Parent proprietà oppure che il codice possa eseguire un ciclo infinito.

Si applica a

Vedi anche