HtmlElementCollection.GetElementsByName(String) 方法

定义

根据名称获取元素的集合。

public:
 System::Windows::Forms::HtmlElementCollection ^ GetElementsByName(System::String ^ name);
public System.Windows.Forms.HtmlElementCollection GetElementsByName (string name);
member this.GetElementsByName : string -> System.Windows.Forms.HtmlElementCollection
Public Function GetElementsByName (name As String) As HtmlElementCollection

参数

name
String

元素的名称或 ID。

返回

HtmlElementCollection

HtmlElementCollection,包含其 Name 属性与 name 匹配的元素。

示例

下面的代码示例使用其名称查找对象 FORM ,并通过编程方式将其数据提交到服务器。 The code example requires that your application hosts a WebBrowser control named webBrowser1.

private void SubmitForm(String formName)
{
    HtmlElementCollection elems = null;
    HtmlElement elem = null;

    if (webBrowser1.Document != null)
    {
        HtmlDocument doc = webBrowser1.Document;
        elems = doc.All.GetElementsByName(formName);
        if (elems != null && elems.Count > 0)
        {
            elem = elems[0];
            if (elem.TagName.Equals("FORM"))
            {
                elem.InvokeMember("Submit");
            }
        }
    }
}
Private Sub SubmitForm(ByVal FormName As String)
    Dim Elems As HtmlElementCollection
    Dim Elem As HtmlElement

    If (WebBrowser1.Document IsNot Nothing) Then
        With WebBrowser1.Document
            Elems = .All.GetElementsByName(FormName)
            If (Not Elems Is Nothing And Elems.Count > 0) Then
                Elem = Elems(0)
                If (Elem.TagName.Equals("FORM")) Then
                    Elem.InvokeMember("Submit")
                End If
            End If
        End With
    End If
End Sub

注解

Id虽然属性必须是唯一HtmlElement的,但多个元素可以使用同一Name属性。

适用于