HtmlElement.InsertAdjacentElement 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在文件物件模型(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 orientation, 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 orientation, 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
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
Public Function InsertAdjacentElement (orientation As HtmlElementInsertionOrientation, newElement As HtmlElement) As HtmlElement
參數
- orientorientation
- HtmlElementInsertionOrientation
該如何插入此元素相對於當前元素的位置。
- newElement
- HtmlElement
新增的元素。
傳回
那 HtmlElement 個剛剛才才插入。 若插入失敗,則會返回 null。
範例
以下程式碼範例會在使用者在 ADatum.com 伺服器外的每個頁面頂端插入一個 DIV 元素。 範例要求你的表單包含 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
備註
在控制點事件DocumentCompletedWebBrowser發生之前,不要呼叫此方法。 在此之前呼叫此方法可能會導致異常,因為文件尚未完成載入。
值是否 HtmlElementInsertionOrientation 有效取決於元素的類型。 例如, AfterBegin 若元素是 DIV,則 有效;但若是 SCRIPT 或 IMG 元素,則不成立,且兩者皆不能包含子元素。