HtmlElement.SetAttribute(String, String) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Sets the value of the named attribute on the element.
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)
Parameters
- attributeName
- String
The name of the attribute to set.
- value
- String
The new value of this attribute.
Examples
The following code example adds a new IMG
element to the current document, using SetAttribute to set the SRC
attribute for the image.
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
Remarks
An attribute in HTML is any valid name-value pair for that element. HtmlElement exposes only those attributes that are common to all elements, leaving out those that only apply to certain types of elements; SRC
is a predefined attribute for the IMG
tag, for example, but not for the DIV
tag. Use GetAttribute and SetAttribute to manipulate attributes not exposed on the managed Document Object Model (DOM).
If attributeName
is not a defined attribute on an element, SetAttribute will define it on the element as a new attribute.
GetAttribute and SetAttribute are case-insensitive.
To set the class
attribute on an HtmlElement , you must refer to the attribute as className
when specifying the first argument to SetAttribute
Applies to
See also
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈