HtmlElement.TagName 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得 HTML 標籤名稱。
public:
property System::String ^ TagName { System::String ^ get(); };
public string TagName { get; }
member this.TagName : string
Public ReadOnly Property TagName As String
屬性值
這個名稱是用來使用 HTML 標記建立這個元素的。
範例
以下程式碼範例會找到文件中的所有 IMG 標籤,並利用該 TagName 屬性測試是否 IMG 已超連結到其他頁面;若有,程式碼會將網址指派給 ALT 標籤屬性 IMG ,使用者可將滑鼠移至圖片上查看將導向何處。
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"));
}
}
}
}
}
Private Sub AddUrlToTooltip()
If (WebBrowser1.Document IsNot Nothing) Then
With WebBrowser1.Document
For Each Elem As HtmlElement In .GetElementsByTagName("IMG")
If (Elem.Parent.TagName.Equals("A")) Then
Dim AltStr As String = Elem.GetAttribute("ALT")
If (Not (AltStr Is Nothing) And (AltStr.Length <> 0)) Then
Elem.SetAttribute("ALT", AltStr & " - points to " & Elem.Parent.GetAttribute("HREF"))
Else
Elem.SetAttribute("ALT", "Points to " & Elem.Parent.GetAttribute("HREF"))
End If
End If
Next
End With
End If
End Sub
備註
HTML 文件物件模型中的許多元素擁有這些元素獨一無二的屬性、屬性和方法;例如 HREF 元素上的 A 屬性,或 Submit 的方法 FORM。 當你有一個潛在任意型別的元素,需要執行特定型別的操作時使用 TagName 。