HtmlElement HtmlElement HtmlElement HtmlElement Class

定义

表示网页内的一个 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
继承
HtmlElementHtmlElementHtmlElementHtmlElement

示例

下面的代码示例演示如何检查任意 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名为控件WebBrowser1This 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

注解

HtmlElement 表示 HTML 文档中的元素的任何可能的类型,如BODYTABLE,和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. 使用CanHaveChildren属性来测试是否给定的元素有子级,和Children要循环访问这些集合。Use 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,如SRC特性,可以在IMG元素或Submit方法FORMYou 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. GetAttributeSetAttribute方法使您检索和修改任何属性或属性上的特定元素,而InvokeMember提供任何方法不会公开中托管的文档对象模型 (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方法HtmlDocument,并将其添加到另一个元素使用AppendChildInsertAdjacentElement方法。You 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 All All All

获取当前元素下的所有元素的 HtmlElementCollectionGets an HtmlElementCollection of all elements underneath the current element.

CanHaveChildren CanHaveChildren CanHaveChildren CanHaveChildren

获取一个值,该值指示此元素是否可以拥有子元素。Gets a value indicating whether this element can have child elements.

Children Children Children Children

获取当前元素的所有子级的 HtmlElementCollectionGets an HtmlElementCollection of all children of the current element.

ClientRectangle ClientRectangle ClientRectangle ClientRectangle

获取 HTML 文档中元素工作区的边界。Gets the bounds of the client area of the element in the HTML document.

Document Document Document Document

获取此元素所属的 HtmlDocumentGets the HtmlDocument to which this element belongs.

DomElement DomElement DomElement DomElement

获取此元素的非托管接口指针。Gets an unmanaged interface pointer for this element.

Enabled Enabled Enabled Enabled

获取或设置用户是否可以向此元素输入数据。Gets or sets whether the user can input data into this element.

FirstChild FirstChild FirstChild FirstChild

获取文档树中此元素下的下一个元素。Gets the next element below this element in the document tree.

Id Id Id Id

获取或设置用于标识此元素的标签。Gets or sets a label by which to identify the element.

InnerHtml InnerHtml InnerHtml InnerHtml

获取或设置此元素下的 HTML 标记。Gets or sets the HTML markup underneath this element.

InnerText InnerText InnerText InnerText

获取或设置分配给此元素的文本。Gets or sets the text assigned to the element.

Name Name Name Name

获取或设置元素的名称。Gets or sets the name of the element.

NextSibling NextSibling NextSibling NextSibling

获取文档树中与此元素位于同一级别的下一个元素。Gets the next element at the same level as this element in the document tree.

OffsetParent OffsetParent OffsetParent OffsetParent

获取从中计算 OffsetRectangle 的元素。Gets the element from which OffsetRectangle is calculated.

OffsetRectangle OffsetRectangle OffsetRectangle OffsetRectangle

获取元素相对于其父级的位置。Gets the location of an element relative to its parent.

OuterHtml OuterHtml OuterHtml OuterHtml

获取或设置当前元素的 HTML 代码。Gets or sets the current element's HTML code.

OuterText OuterText OuterText OuterText

获取或设置当前元素的文本。Gets or sets the current element's text.

Parent Parent Parent Parent

获取当前元素的父元素。Gets the current element's parent element.

ScrollLeft ScrollLeft ScrollLeft ScrollLeft

获取或设置元素边缘与其内容的左边缘之间的距离。Gets or sets the distance between the edge of the element and the left edge of its content.

ScrollRectangle ScrollRectangle ScrollRectangle ScrollRectangle

获取元素的可滚动区尺寸。Gets the dimensions of an element's scrollable region.

ScrollTop ScrollTop ScrollTop ScrollTop

获取或设置此元素边缘与其内容的上边缘之间的距离。Gets or sets the distance between the edge of the element and the top edge of its content.

Style Style Style Style

获取或设置当前元素的以分号分隔的样式列表。Gets or sets a semicolon-delimited list of styles for the current element.

TabIndex TabIndex TabIndex TabIndex

获取或设置 Tab 键顺序中此元素的位置。Gets or sets the location of this element in the tab order.

TagName TagName TagName TagName

获取 HTML 标记的名称。Gets the name of the HTML tag.

方法

AppendChild(HtmlElement) AppendChild(HtmlElement) AppendChild(HtmlElement) AppendChild(HtmlElement)

将元素添加到其他元素的子树中。Adds an element to another element's subtree.

AttachEventHandler(String, EventHandler) AttachEventHandler(String, EventHandler) AttachEventHandler(String, EventHandler) AttachEventHandler(String, EventHandler)

为 HTML 文档对象模型 (DOM) 上的命名事件添加事件处理程序。Adds an event handler for a named event on the HTML Document Object Model (DOM).

DetachEventHandler(String, EventHandler) DetachEventHandler(String, EventHandler) DetachEventHandler(String, EventHandler) DetachEventHandler(String, EventHandler)

从 HTML 文档对象模型 (DOM) 上的指定事件中移除事件处理程序。Removes an event handler from a named event on the HTML Document Object Model (DOM).

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

测试提供的对象是否等于当前元素。Tests if the supplied object is equal to the current element.

Focus() Focus() Focus() Focus()

将用户输入焦点置于当前元素上。Puts user input focus on the current element.

GetAttribute(String) GetAttribute(String) GetAttribute(String) GetAttribute(String)

检索元素中已命名特性的值。Retrieves the value of the named attribute on the element.

GetElementsByTagName(String) GetElementsByTagName(String) GetElementsByTagName(String) GetElementsByTagName(String)

检索 HTML 中由指定 HTML 标记表示的元素的集合。Retrieves a collection of elements represented in HTML by the specified HTML tag.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

用作特定类型的哈希函数。Serves as a hash function for a particular type.

GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
InsertAdjacentElement(HtmlElementInsertionOrientation, HtmlElement) InsertAdjacentElement(HtmlElementInsertionOrientation, HtmlElement) InsertAdjacentElement(HtmlElementInsertionOrientation, HtmlElement) InsertAdjacentElement(HtmlElementInsertionOrientation, HtmlElement)

将新元素插入到文档对象模型 (DOM) 中。Insert a new element into the Document Object Model (DOM).

InvokeMember(String) InvokeMember(String) InvokeMember(String) InvokeMember(String)

对此元素的基础 DOM 元素执行未公开的方法。Executes an unexposed method on the underlying DOM element of this element.

InvokeMember(String, Object[]) InvokeMember(String, Object[]) InvokeMember(String, Object[]) InvokeMember(String, Object[])

执行在当前 HTML 页中用脚本语言定义的函数。Executes a function defined in the current HTML page by a scripting language.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
RaiseEvent(String) RaiseEvent(String) RaiseEvent(String) RaiseEvent(String)

使指定的事件调用所有已注册的事件处理程序。Causes the named event to call all registered event handlers.

RemoveFocus() RemoveFocus() RemoveFocus() RemoveFocus()

移除当前元素的焦点(如果此元素有焦点)。Removes focus from the current element, if that element has focus.

ScrollIntoView(Boolean) ScrollIntoView(Boolean) ScrollIntoView(Boolean) 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) SetAttribute(String, String) SetAttribute(String, String) SetAttribute(String, String)

设置元素中已命名特性的值。Sets the value of the named attribute on the element.

ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

事件

Click Click Click Click

当用户使用鼠标左键单击此元素时发生。Occurs when the user clicks on the element with the left mouse button.

DoubleClick DoubleClick DoubleClick DoubleClick

当用户用鼠标左键快速连续单击元素两次时发生。Occurs when the user clicks the left mouse button over an element twice, in rapid succession.

Drag Drag Drag Drag

当用户将文本拖动到各个位置时发生。Occurs when the user drags text to various locations.

DragEnd DragEnd DragEnd DragEnd

当用户完成拖动操作时发生。Occurs when a user finishes a drag operation.

DragLeave DragLeave DragLeave DragLeave

当用户不再在此元素上拖动项时发生。Occurs when the user is no longer dragging an item over this element.

DragOver DragOver DragOver DragOver

当用户将文本拖动到元素上时发生。Occurs when the user drags text over the element.

Focusing Focusing Focusing Focusing

当元素首次接收用户输入焦点时发生。Occurs when the element first receives user input focus.

GotFocus GotFocus GotFocus GotFocus

当元素已收到用户输入焦点时发生。Occurs when the element has received user input focus.

KeyDown KeyDown KeyDown KeyDown

当用户按下键盘上的某个键时发生。Occurs when the user presses a key on the keyboard.

KeyPress KeyPress KeyPress KeyPress

当用户按下并释放键盘上的某个键时发生。Occurs when the user presses and releases a key on the keyboard.

KeyUp KeyUp KeyUp KeyUp

当用户释放键盘上的某个键时发生。Occurs when the user releases a key on the keyboard.

LosingFocus LosingFocus LosingFocus LosingFocus

当元素正在失去用户输入焦点时发生。Occurs when the element is losing user input focus.

LostFocus LostFocus LostFocus LostFocus

当元素已失去用户输入焦点时发生。Occurs when the element has lost user input focus.

MouseDown MouseDown MouseDown MouseDown

当用户按下鼠标按钮时发生。Occurs when the user presses a mouse button.

MouseEnter MouseEnter MouseEnter MouseEnter

当用户首次将鼠标光标移动到当前元素上时发生。Occurs when the user first moves the mouse cursor over the current element.

MouseLeave MouseLeave MouseLeave MouseLeave

当用户将鼠标光标从当前元素上移开时发生。Occurs when the user moves the mouse cursor off of the current element.

MouseMove MouseMove MouseMove MouseMove

当用户将鼠标光标通过元素时发生。Occurs when the user moves the mouse cursor across the element.

MouseOver MouseOver MouseOver MouseOver

当鼠标光标进入元素的边界时发生。Occurs when the mouse cursor enters the bounds of the element.

MouseUp MouseUp MouseUp MouseUp

当用户释放鼠标按钮时发生。Occurs when the user releases a mouse button.

操作员

Equality(HtmlElement, HtmlElement) Equality(HtmlElement, HtmlElement) Equality(HtmlElement, HtmlElement) Equality(HtmlElement, HtmlElement)

比较两个元素是否相等。Compares two elements for equality.

Inequality(HtmlElement, HtmlElement) Inequality(HtmlElement, HtmlElement) Inequality(HtmlElement, HtmlElement) Inequality(HtmlElement, HtmlElement)

比较两个 HtmlElement 对象是否相等。Compares two HtmlElement objects for inequality.

适用于

另请参阅