HtmlElement クラス
定義
Web ページ内の HTML 要素を表します。Represents an HTML element inside of a Web page.
public ref class HtmlElement sealed
public sealed class HtmlElement
type HtmlElement = class
Public NotInheritable Class HtmlElement
- 継承
-
HtmlElement
例
次のコード例では、任意の HTML ドキュメントを調べて、HTML 要素を記述する文字列を派生させます。この文字列は、ドキュメント内の要素が入れ子になっている深さを示すインデントとレベル番号を使用します。The following code example shows how to examine an arbitrary HTML document and derive a string describing the HTML elements, with indentation and level numbers used to indicate how deeply nested the elements are in the document. このコード例では、アプリケーションがとWebBrowserいう名前WebBrowser1
のコントロールをホストする必要があります。This code example requires that your application hosts a WebBrowser control named WebBrowser1
.
private void PrintDomBegin()
{
if (webBrowser1.Document != null)
{
HtmlElementCollection elemColl = null;
HtmlDocument doc = webBrowser1.Document;
if (doc != null)
{
elemColl = doc.GetElementsByTagName("HTML");
String str = PrintDom(elemColl, new System.Text.StringBuilder(), 0);
webBrowser1.DocumentText = str;
}
}
}
private string PrintDom(HtmlElementCollection elemColl, System.Text.StringBuilder returnStr, Int32 depth)
{
System.Text.StringBuilder str = new System.Text.StringBuilder();
foreach (HtmlElement elem in elemColl)
{
string elemName;
elemName = elem.GetAttribute("ID");
if (elemName == null || elemName.Length == 0)
{
elemName = elem.GetAttribute("name");
if (elemName == null || elemName.Length == 0)
{
elemName = "<no name>";
}
}
str.Append(' ', depth * 4);
str.Append(elemName + ": " + elem.TagName + "(Level " + depth + ")");
returnStr.AppendLine(str.ToString());
if (elem.CanHaveChildren)
{
PrintDom(elem.Children, returnStr, depth + 1);
}
str.Remove(0, str.Length);
}
return(returnStr.ToString());
}
Private Sub PrintDomBegin()
If (WebBrowser1.Document IsNot Nothing) Then
Dim ElemColl As HtmlElementCollection
Dim Doc As HtmlDocument = WebBrowser1.Document
If (Not (Doc Is Nothing)) Then
ElemColl = Doc.GetElementsByTagName("HTML")
Dim Str As String = PrintDom(ElemColl, New System.Text.StringBuilder(), 0)
WebBrowser1.DocumentText = Str
End If
End If
End Sub
Private Function PrintDom(ByVal ElemColl As HtmlElementCollection, ByRef ReturnStr As System.Text.StringBuilder, ByVal Depth As Integer) As String
Dim Str As New System.Text.StringBuilder()
For Each Elem As HtmlElement In ElemColl
Dim ElemName As String
ElemName = Elem.GetAttribute("ID")
If (ElemName Is Nothing Or ElemName.Length = 0) Then
ElemName = Elem.GetAttribute("name")
If (ElemName Is Nothing Or ElemName.Length = 0) Then
ElemName = "<no name>"
End If
End If
Str.Append(CChar(" "), Depth * 4)
Str.Append(ElemName & ": " & Elem.TagName & "(Level " & Depth & ")")
ReturnStr.AppendLine(Str.ToString())
If (Elem.CanHaveChildren) Then
PrintDom(Elem.Children, ReturnStr, Depth + 1)
End If
Str.Remove(0, Str.Length)
Next
PrintDom = ReturnStr.ToString()
End Function
注釈
HtmlElementBODY
、TABLE
、など、HTMLドキュメント内の任意の型の要素を表します。FORM
HtmlElement represents any possible type of element in an HTML document, such as BODY
, TABLE
, and FORM
, among others. クラスは、すべての要素で検索することが予想される最も一般的なプロパティを公開します。The class exposes the most common properties you can expect to find on all elements.
ほとんどの要素には子要素があり、その下に配置されている他の HTML 要素を使用できます。Most elements can have child elements: other HTML elements that are placed underneath them. プロパティを使用して、指定された要素に子Childrenがあるかどうかをテストし、コレクションを反復処理します。 CanHaveChildrenUse the CanHaveChildren property to test whether a given element has children, and the Children collection to iterate through them. プロパティParentは、現在HtmlElementの要素が入れ子になっているを返します。The Parent property returns the HtmlElement in which the current element is nested.
多くの場合、 HtmlElement IMG
要素のSRC
属性やSubmit
のメソッドFORM
など、によって直接公開されていない、基になる要素の属性、プロパティ、およびメソッドへのアクセスが必要になります。You often need access to attributes, properties, and methods on the underlying element that are not directly exposed by HtmlElement, such as the SRC
attribute on an IMG
element or the Submit
method on a FORM
. メソッドGetAttribute InvokeMemberとSetAttributeメソッドを使用すると、特定の要素の任意の属性またはプロパティを取得および変更できます。一方、はマネージドキュメントオブジェクトモデル (DOM) に公開されていないメソッドへのアクセスを提供します。The GetAttribute and SetAttribute methods enable you to retrieve and alter any attribute or property on a specific element, while InvokeMember provides access to any methods not exposed in the managed Document Object Model (DOM). アプリケーションにアンマネージコードのアクセス許可がある場合は、 DomElement属性を使用して、非公開のプロパティおよびメソッドにアクセスすることもできます。If your application has unmanaged code permission, you can also access unexposed properties and methods with the DomElement attribute.
プロパティをTagName使用して、要素が特定の型であるかどうかをテストします。Use the TagName property to test whether an element is of a specific type.
任意の HTML ドキュメントを実行時に変更できます。Any HTML document can be modified at run time. のHtmlElement CreateElement AppendChildメソッドを使用して新しいオブジェクトを作成し、メソッドまたはInsertAdjacentElementメソッドを使用して別の要素に追加することができます。 HtmlDocumentYou can create new HtmlElement objects with the CreateElement method of HtmlDocument, and add them to another element using the AppendChild or InsertAdjacentElement methods. また、要素を HTML タグとして作成し、既存の要素のInnerHtmlプロパティに割り当てることもできます。You can also create the elements as HTML tags and assign them to an existing element's InnerHtml property.
プロパティ
All |
現在の要素の下位にあるすべての要素の HtmlElementCollection を取得します。Gets an HtmlElementCollection of all elements underneath the current element. |
CanHaveChildren |
この要素が子要素を持つことができるかどうかを示す値を取得します。Gets a value indicating whether this element can have child elements. |
Children |
現在の要素のすべての子の HtmlElementCollection を取得します。Gets an HtmlElementCollection of all children of the current element. |
ClientRectangle |
HTML ドキュメントにおける要素のクライアント領域の境界を取得します。Gets the bounds of the client area of the element in the HTML document. |
Document |
この要素が属する HtmlDocument を取得します。Gets the HtmlDocument to which this element belongs. |
DomElement |
この要素のアンマネージ インターフェイスへのポインターを取得します。Gets an unmanaged interface pointer for this element. |
Enabled |
ユーザーがこの要素にデータを入力できるかどうかを取得または設定します。Gets or sets whether the user can input data into this element. |
FirstChild |
ドキュメント ツリーにおいてこの要素のすぐ下にある次の要素を取得します。Gets the next element below this element in the document tree. |
Id |
要素を識別するラベルを取得または設定します。Gets or sets a label by which to identify the element. |
InnerHtml |
この要素の下位にある HTML マークアップを取得または設定します。Gets or sets the HTML markup underneath this element. |
InnerText |
要素に割り当てられたテキストを取得または設定します。Gets or sets the text assigned to the element. |
Name |
要素の名前を取得または設定します。Gets or sets the name of the element. |
NextSibling |
ドキュメント ツリーにおいてこの要素と同じレベルにある次の要素を取得します。Gets the next element at the same level as this element in the document tree. |
OffsetParent |
OffsetRectangle を計算するときの基準となる要素を取得します。Gets the element from which OffsetRectangle is calculated. |
OffsetRectangle |
親を基準とした要素の相対的な位置を取得します。Gets the location of an element relative to its parent. |
OuterHtml |
現在の要素の HTML コードを取得または設定します。Gets or sets the current element's HTML code. |
OuterText |
現在の要素のテキストを取得または設定します。Gets or sets the current element's text. |
Parent |
現在の要素の親要素を取得します。Gets the current element's parent element. |
ScrollLeft |
要素の端とその内容の左端との距離を取得または設定します。Gets or sets the distance between the edge of the element and the left edge of its content. |
ScrollRectangle |
要素のスクロール可能な領域の寸法を取得します。Gets the dimensions of an element's scrollable region. |
ScrollTop |
要素の端とその内容の上端との距離を取得または設定します。Gets or sets the distance between the edge of the element and the top edge of its content. |
Style |
現在の要素のスタイルのセミコロン区切りリストを取得または設定します。Gets or sets a semicolon-delimited list of styles for the current element. |
TabIndex |
この要素のタブ オーダーにおける位置を取得または設定します。Gets or sets the location of this element in the tab order. |
TagName |
HTML タグの名前を取得します。Gets the name of the HTML tag. |
メソッド
AppendChild(HtmlElement) |
要素を別の要素のサブツリーに追加します。Adds an element to another element's subtree. |
AttachEventHandler(String, EventHandler) |
HTML DOM の指定したイベントにイベント ハンドラーを追加します。Adds an event handler for a named event on the HTML Document Object Model (DOM). |
DetachEventHandler(String, EventHandler) |
HTML DOM の指定したイベントからイベント ハンドラーを削除します。Removes an event handler from a named event on the HTML Document Object Model (DOM). |
Equals(Object) |
指定したオブジェクトが現在の要素と等しいかどうかをテストします。Tests if the supplied object is equal to the current element. |
Focus() |
ユーザーの入力フォーカスを現在の要素に設定します。Puts user input focus on the current element. |
GetAttribute(String) |
要素の名前付き属性の値を取得します。Retrieves the value of the named attribute on the element. |
GetElementsByTagName(String) |
|
GetHashCode() |
特定の型のハッシュ関数として機能します。Serves as a hash function for a particular type. |
GetType() |
現在のインスタンスの Type を取得します。Gets the Type of the current instance. (継承元 Object) |
InsertAdjacentElement(HtmlElementInsertionOrientation, HtmlElement) |
新しい要素を DOM に挿入します。Insert a new element into the Document Object Model (DOM). |
InvokeMember(String) |
この要素の基になる DOM 要素に対して非公開メソッドを実行します。Executes an unexposed method on the underlying DOM element of this element. |
InvokeMember(String, Object[]) |
現在の HTML ページにスクリプト言語で定義されている関数を実行します。Executes a function defined in the current HTML page by a scripting language. |
MemberwiseClone() |
現在の Object の簡易コピーを作成します。Creates a shallow copy of the current Object. (継承元 Object) |
RaiseEvent(String) |
指定したイベントを発生させて、すべての登録されたイベント ハンドラーを呼び出します。Causes the named event to call all registered event handlers. |
RemoveFocus() |
現在の要素にフォーカスがある場合、そのフォーカスを要素から削除します。Removes focus from the current element, if that element has focus. |
ScrollIntoView(Boolean) |
この要素の最上端または最下端がドキュメントのウィンドウに位置合わせされるまで、この要素を含むドキュメントをスクロールします。Scrolls through the document containing this element until the top or bottom edge of this element is aligned with the document's window. |
SetAttribute(String, String) |
要素の名前付き属性の値を設定します。Sets the value of the named attribute on the element. |
ToString() |
現在のオブジェクトを表す string を返します。Returns a string that represents the current object. (継承元 Object) |
演算子
Equality(HtmlElement, HtmlElement) |
2 つの要素が等しいかどうかを比較します。Compares two elements for equality. |
Inequality(HtmlElement, HtmlElement) |
2 つの HtmlElement オブジェクトが等しくないかどうかを比較します。Compares two HtmlElement objects for inequality. |
イベント
Click |
ユーザーが要素をマウスの左ボタンでクリックしたときに発生します。Occurs when the user clicks on the element with the left mouse button. |
DoubleClick |
ユーザーが要素上でマウスの左ボタンを 2 回すばやく連続的にクリックしたときに発生します。Occurs when the user clicks the left mouse button over an element twice, in rapid succession. |
Drag |
ユーザーがテキストをさまざまな位置にドラッグしたときに発生します。Occurs when the user drags text to various locations. |
DragEnd |
ユーザーがドラッグ操作を終了したときに発生します。Occurs when a user finishes a drag operation. |
DragLeave |
ユーザーがこの要素上への項目のドラッグを終了したときに発生します。Occurs when the user is no longer dragging an item over this element. |
DragOver |
ユーザーがテキストを要素上にドラッグしたときに発生します。Occurs when the user drags text over the element. |
Focusing |
要素が初めてユーザーの入力フォーカスを得たときに発生します。Occurs when the element first receives user input focus. |
GotFocus |
要素がユーザーの入力フォーカスを得たときに発生します。Occurs when the element has received user input focus. |
KeyDown |
ユーザーがキーボードのキーを押したときに発生します。Occurs when the user presses a key on the keyboard. |
KeyPress |
ユーザーがキーボードのキーを押して離したときに発生します。Occurs when the user presses and releases a key on the keyboard. |
KeyUp |
ユーザーがキーボードのキーを離したときに発生します。Occurs when the user releases a key on the keyboard. |
LosingFocus |
要素がユーザーの入力フォーカスを失うときに発生します。Occurs when the element is losing user input focus. |
LostFocus |
要素がユーザーの入力フォーカスを失ったときに発生します。Occurs when the element has lost user input focus. |
MouseDown |
ユーザーがマウス ボタンを押したときに発生します。Occurs when the user presses a mouse button. |
MouseEnter |
ユーザーがマウス カーソルを現在の要素上に初めて移動させたときに発生します。Occurs when the user first moves the mouse cursor over the current element. |
MouseLeave |
ユーザーがマウス カーソルを現在の要素から移動させたときに発生します。Occurs when the user moves the mouse cursor off of the current element. |
MouseMove |
ユーザーがマウス カーソルを要素上に通過させたときに発生します。Occurs when the user moves the mouse cursor across the element. |
MouseOver |
マウス カーソルが要素の境界内に入ったときに発生します。Occurs when the mouse cursor enters the bounds of the element. |
MouseUp |
ユーザーがマウス ボタンを離したときに発生します。Occurs when the user releases a mouse button. |