HtmlElement.InsertAdjacentElement 메서드

정의

새 요소를 DOM(문서 개체 모델)에 삽입합니다.

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

매개 변수

orient
HtmlElementInsertionOrientation

현재 요소를 기준으로 이 요소를 삽입할 위치입니다.

newElement
HtmlElement

삽입할 새 요소입니다.

반환

방금 삽입한 HtmlElement 입니다. 삽입이 실패한 경우 null이 반환됩니다.

예제

다음 코드 예제에서는 삽입을 DIV ADatum.com 서버 외부에서 볼 수 있는 모든 페이지의 맨 위에 있는 요소입니다. 폼이 예제는 WebBrowser 라는 컨트롤 WebBrowser1합니다. 샘플은 네임스페이스 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

설명

컨트롤의 이벤트가 발생할 때까지 DocumentCompleted 이 메서드를 WebBrowser 호출하지 마세요. 그 전에 이 메서드를 호출하면 문서 로드가 완료되지 않으므로 예외가 발생할 수 있습니다.

HtmlElementInsertionOrientation 이 유효한지 여부는 요소의 형식에 따라 달라집니다. 예를 들어 는 AfterBegin 요소가 DIV인 경우 유효하지만 또는 요소인 경우에는 SCRIPTIMG 유효하지 않으며 둘 다 자식 요소를 포함할 수 없습니다.

적용 대상

추가 정보