HtmlElement.TagName Property

Definition

Gets the name of the HTML tag.

C#
public string TagName { get; }

Property Value

The name used to create this element using HTML markup.

Examples

The following code example finds all of the IMG tags in a document, and uses the TagName property to test whether the IMG is hyperlinked to another page; if it is, the code assigns the URL to the ALT attribute of the IMG tag, so that users can mouse over the image to see where it will take them.

C#
private void AddUrlToTooltip()
{
    if (webBrowser1.Document != null)
    {
        foreach (HtmlElement elem in webBrowser1.Document.GetElementsByTagName("IMG"))
        {
            if (elem.Parent.TagName.Equals("A"))
            {
                String altStr = elem.GetAttribute("ALT");
                if (!(altStr == null) && (altStr.Length != 0))
                {
                    elem.SetAttribute("ALT", altStr + " - points to " + elem.Parent.GetAttribute("HREF"));
                }
                else
                {
                    elem.SetAttribute("ALT", "Points to " + elem.Parent.GetAttribute("HREF"));
                }
            }
        }
    }
}

Remarks

Many elements in the HTML Document Object Model have attributes, properties, and methods that are unique to those elements; such as the HREF attribute on the A element, or the Submit method on FORM. Use TagName when you have an element of a potentially arbitrary type, and need to perform a type-specific operation.

Applies to

제품 버전
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

See also