HtmlElement.InsertAdjacentElement(HtmlElementInsertionOrientation, HtmlElement) HtmlElement.InsertAdjacentElement(HtmlElementInsertionOrientation, HtmlElement) HtmlElement.InsertAdjacentElement(HtmlElementInsertionOrientation, HtmlElement) HtmlElement.InsertAdjacentElement(HtmlElementInsertionOrientation, HtmlElement) Method

定义

将新元素插入到文档对象模型 (DOM) 中。Insert a new element into the Document Object Model (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);
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 HtmlElementInsertionOrientation HtmlElementInsertionOrientation HtmlElementInsertionOrientation

此元素要插入到的相对于当前元素的位置。Where to insert this element in relation to the current element.

newElement
HtmlElement HtmlElement HtmlElement HtmlElement

要插入的新元素。The new element to insert.

返回

刚才插入的 HtmlElementThe HtmlElement that was just inserted. 如果插入失败,将返回 nullIf insertion failed, this will return null.

示例

下面的代码示例中插入DIV到 ADatum.com server 外部的用户查看每个页面顶部的元素。The following code example inserts a DIV element into the top of every page that users view outside of the ADatum.com server. 该示例需要您的窗体包含WebBrowser名为控件WebBrowser1The example requires that your form contains a WebBrowser control named WebBrowser1. 您的示例还必须导入命名空间System.Text.RegularExpressionsYour sample must also import the namespace 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控件已发生。Do not call this method until after the DocumentCompleted event on the WebBrowser control has occurred. 前调用此方法可能会导致异常,因为该文档将未完成加载。Calling this method before then can result in an exception, as the document will not have finished loading.

值是否HtmlElementInsertionOrientation是否有效取决于元素的类型。Whether a value of HtmlElementInsertionOrientation is valid will depend on the type of the element. 例如,AfterBegin无效,如果该元素是DIV,但如果它是不SCRIPTIMG元素,这两者都不可以包含子元素。For example, AfterBegin is valid if the element is a DIV, but not if it is a SCRIPT or IMG element, neither of which can contain child elements.

适用于

另请参阅