HtmlElement.SetAttribute(String, String) 方法

定義

設定此項目上具名屬性的值。

public:
 void SetAttribute(System::String ^ attributeName, System::String ^ value);
public void SetAttribute (string attributeName, string value);
member this.SetAttribute : string * string -> unit
Public Sub SetAttribute (attributeName As String, value As String)

參數

attributeName
String

要設定的屬性名稱。

value
String

這個屬性的新值。

範例

下列程式碼範例會使用 SetAttribute 來設定 SRC 影像的 屬性,將新 IMG 元素加入至目前的檔。

private void InsertImageFooter()
{
    if (webBrowser1.Document != null)
    {
        HtmlDocument doc = webBrowser1.Document;
        HtmlElement elem = doc.CreateElement("IMG");
        elem.SetAttribute("SRC", "http://www.adatum.com/images/footer-banner.jpg");

        doc.Body.AppendChild(elem);
    }
}
Private Sub InsertImageFooter()
    If (WebBrowser1.Document IsNot Nothing) Then
        With WebBrowser1.Document
            Dim Elem As HtmlElement = .CreateElement("IMG")
            Elem.SetAttribute("SRC", "http://www.adatum.com/images/footer-banner.jpg")

            .Body.AppendChild(Elem)
        End With
    End If
End Sub

備註

HTML 中的屬性是該專案的任何有效名稱/值組。 HtmlElement 只會公開所有元素通用的屬性,只排除只套用至特定類型專案的屬性; SRC 是標記的 IMG 預先定義屬性,例如,而不是標記的 DIV 。 使用 GetAttributeSetAttribute 操作未在 Managed Document Object Model 上公開的屬性, (DOM) 。

如果 attributeName 不是元素上的已定義屬性, SetAttribute 則會將元素上的定義屬性定義為新的屬性。

GetAttributeSetAttribute 不區分大小寫。

若要在 上 HtmlElement 設定 class 屬性,您必須在指定第一個引數時參考 屬性, classNameSetAttribute

適用於

另請參閱