HtmlElement.GetAttribute(String) 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
检索元素中已命名特性的值。
public:
 System::String ^ GetAttribute(System::String ^ attributeName);public string GetAttribute(string attributeName);member this.GetAttribute : string -> stringPublic Function GetAttribute (attributeName As String) As String参数
- attributeName
- String
属性名。 此自变量区分大小写。
返回
元素中此特性 String 形式的值。 如果此元素中不存在指定的特性,则返回一个空字符串。
示例
下面的代码示例检索 HTML 文档中的所有 META 标记,使用 GetAttribute 查找 META 名称 Description为 的标记。 该示例要求应用程序具有名为 WebBrowser 的 WebBrowser1控件。
private void DisplayMetaDescription()
{
    if (webBrowser1.Document != null)
    {
        HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("META");
        foreach (HtmlElement elem in elems)
        {
            String nameStr = elem.GetAttribute("name");
            if (nameStr != null && nameStr.Length != 0)
            {
                String contentStr = elem.GetAttribute("content");
                MessageBox.Show("Document: " + webBrowser1.Url.ToString() + "\nDescription: " + contentStr);
            }
        }
    }
}
Private Sub DisplayMetaDescription()
    If (WebBrowser1.Document IsNot Nothing) Then
        Dim Elems As HtmlElementCollection
        Dim WebOC As WebBrowser = WebBrowser1
        Elems = WebOC.Document.GetElementsByTagName("META")
        For Each elem As HtmlElement In Elems
            Dim NameStr As String = elem.GetAttribute("name")
            If ((NameStr IsNot Nothing) And (NameStr.Length <> 0)) Then
                If NameStr.ToLower().Equals("description") Then
                    Dim ContentStr As String = elem.GetAttribute("content")
                    MessageBox.Show("Document: " & WebOC.Url.ToString() & vbCrLf & "Description: " & ContentStr)
                End If
            End If
        Next
    End If
End Sub
注解
HTML 中的属性是该元素的任何有效名称/值对。 
              HtmlElement 仅公开所有元素共有的属性,而省去仅适用于某些类型的元素的属性; SRC 例如,是标记的 IMG 预定义属性,但不是标记的 DIV 预定义属性。 使用 GetAttribute 和 SetAttribute 操作未在托管文档对象模型 (DOM) 上公开的属性。
GetAttribute 和 SetAttribute 不区分大小写。