HtmlElement.InsertAdjacentElement メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > HtmlElement.InsertAdjacentElement メソッドの意味・解説 

HtmlElement.InsertAdjacentElement メソッド

メモ : このメソッドは、.NET Framework version 2.0 で新しく追加されたものです。

新しい要素を DOM に挿入します。

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

Public Function InsertAdjacentElement ( _
    orient As HtmlElementInsertionOrientation, _
    newElement As HtmlElement _
) As HtmlElement
Dim instance As HtmlElement
Dim orient As HtmlElementInsertionOrientation
Dim newElement As HtmlElement
Dim returnValue As HtmlElement

returnValue = instance.InsertAdjacentElement(orient, newElement)
public HtmlElement InsertAdjacentElement (
    HtmlElementInsertionOrientation orient,
    HtmlElement newElement
)
public:
HtmlElement^ InsertAdjacentElement (
    HtmlElementInsertionOrientation orient, 
    HtmlElement^ newElement
)
public HtmlElement InsertAdjacentElement (
    HtmlElementInsertionOrientation orient, 
    HtmlElement newElement
)
public function InsertAdjacentElement (
    orient : HtmlElementInsertionOrientation, 
    newElement : HtmlElement
) : HtmlElement

パラメータ

orient

現在の要素を基準とした、この要素の挿入位置。

newElement

挿入する新しい要素。

戻り値
挿入された HtmlElement。挿入が失敗した場合は null 参照 (Visual Basic では Nothing) を返します。

解説解説

このメソッドは、WebBrowser コントロールの DocumentCompleted イベントが発生するまで呼び出さないでください。その前にこのメソッドを呼び出すと、ドキュメントの読み込みが終了していないため、例外がスローされます。

HtmlElementInsertionOrientation の値が有効であるかどうかは、要素の種類によって異なります。たとえば、AfterBegin は DIV 要素については有効ですが、SCRIPT 要素と IMG 要素については有効ではありません。これは、これらの要素がどちらも子要素を持つことができないからです。

使用例使用例

次のコード例では、ユーザーが ADatum.com サーバー以外から表示するすべてのページの最上部に、DIV 要素を挿入します。この例では、フォームに WebBrowser1 という名前の WebBrowser コントロールが含まれている必要があります。また、名前空間 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);
    }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

HtmlElement.InsertAdjacentElement メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



HtmlElement.InsertAdjacentElement メソッドのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS