HtmlElement.InsertAdjacentElement Metodo

Definizione

Inserisce un nuovo elemento nel DOM (Document Object Model).

public:
 System::Windows::Forms::HtmlElement ^ InsertAdjacentElement(System::Windows::Forms::HtmlElementInsertionOrientation orient, System::Windows::Forms::HtmlElement ^ newElement);
public System.Windows.Forms.HtmlElement InsertAdjacentElement (System.Windows.Forms.HtmlElementInsertionOrientation orient, System.Windows.Forms.HtmlElement newElement);
public System.Windows.Forms.HtmlElement? InsertAdjacentElement (System.Windows.Forms.HtmlElementInsertionOrientation orient, System.Windows.Forms.HtmlElement newElement);
member this.InsertAdjacentElement : System.Windows.Forms.HtmlElementInsertionOrientation * System.Windows.Forms.HtmlElement -> System.Windows.Forms.HtmlElement
Public Function InsertAdjacentElement (orient As HtmlElementInsertionOrientation, newElement As HtmlElement) As HtmlElement

Parametri

orient
HtmlElementInsertionOrientation

Posizione in cui viene inserito questo elemento in relazione all'elemento corrente.

newElement
HtmlElement

Nuovo elemento da inserire.

Restituisce

Oggetto HtmlElement appena inserito. Se l'inserimento ha esito negativo, verrà restituito il valore null.

Esempio

Nell'esempio di codice seguente viene inserito un DIV elemento nella parte superiore di ogni pagina visualizzata dagli utenti all'esterno del server ADatum.com. L'esempio richiede che il modulo contenga un WebBrowser controllo denominato WebBrowser1. L'esempio deve anche importare lo spazio dei nomi System.Text.RegularExpressions.

public void AddDivMessage()
{
    Uri currentUri = new Uri(webBrowser1.Url.ToString());
    String hostName = null;

    // Ensure we have a host name, and not just an IP, against which to test.
    if (!(currentUri.HostNameType == UriHostNameType.Dns))
    {
        DnsPermission permit = new DnsPermission(System.Security.Permissions.PermissionState.Unrestricted);
        permit.Assert();

        IPHostEntry hostEntry = System.Net.Dns.GetHostEntry(currentUri.Host);
        hostName = hostEntry.HostName;
    }
    else
    {
        hostName = currentUri.Host;
    }

    if (!hostName.Contains("adatum.com"))
    {
        AddTopPageMessage("You are viewing a web site other than ADatum.com. " +
            "Please exercise caution, and ensure your Web surfing complies with all " +
            "corporate regulations as laid out in the company handbook.");
    }
}

private void AddTopPageMessage(String message)
{
    if (webBrowser1.Document != null)
    {
        HtmlDocument doc = webBrowser1.Document;

        // Do not insert the warning again if it already exists. 
        HtmlElementCollection returnedElems = doc.All.GetElementsByName("ADatumWarningDiv");
        if ((returnedElems != null) && (returnedElems.Count > 0))
        {
            return;
        }

        HtmlElement divElem = doc.CreateElement("DIV");
        divElem.Name = "ADatumWarningDiv";
        divElem.Style = "background-color:black;color:white;font-weight:bold;width:100%;";
        divElem.InnerText = message;

        divElem = doc.Body.InsertAdjacentElement(HtmlElementInsertionOrientation.AfterBegin, divElem);
    }
}
Private Sub AddDivMessage()
    Dim CurrentUri As New Uri(WebBrowser1.Url.ToString())
    Dim HostName As String

    ' Ensure we have a host name, and not just an IP, against which to test.
    If (Not CurrentUri.HostNameType = UriHostNameType.Dns) Then
        Dim Permit As New DnsPermission(System.Security.Permissions.PermissionState.Unrestricted)
        Permit.Assert()

        Dim HostEntry As IPHostEntry = System.Net.Dns.GetHostEntry(CurrentUri.Host)
        HostName = HostEntry.HostName
    Else
        HostName = CurrentUri.Host
    End If

    If (Not HostName.Contains("adatum.com")) Then
        AddTopPageMessage("You are viewing a web site other than ADatum.com. " & _
            "Please exercise caution, and ensure your web surfing complies with all " & _
            "corporate regulations as laid out in the company handbook.")
    End If
End Sub

Private Sub AddTopPageMessage(ByVal Message As String)
    If (WebBrowser1.Document IsNot Nothing) Then
        With WebBrowser1.Document
            ' Do not insert the warning again if it already exists. 
            Dim ReturnedElems As HtmlElementCollection = .All.GetElementsByName("ADatumWarningDiv")
            If (Not (ReturnedElems Is Nothing) And (ReturnedElems.Count > 0)) Then
                Exit Sub
            End If

            Dim DivElem As HtmlElement = .CreateElement("DIV")
            DivElem.Name = "ADatumWarningDiv"
            DivElem.Style = "background-color:black;color:white;font-weight:bold;width:100%;"
            DivElem.InnerText = Message

            DivElem = .Body.InsertAdjacentElement(HtmlElementInsertionOrientation.AfterBegin, DivElem)
        End With
    End If
End Sub

Commenti

Non chiamare questo metodo fino a quando non si è verificato l'evento DocumentCompleted nel WebBrowser controllo. La chiamata a questo metodo prima di poter generare un'eccezione, perché il documento non avrà terminato il caricamento.

Se un valore di HtmlElementInsertionOrientation è valido dipenderà dal tipo dell'elemento. Ad esempio, AfterBegin è valido se l'elemento è , DIVma non se è un SCRIPT elemento o IMG , nessuno dei quali può contenere elementi figlio.

Si applica a

Vedi anche