【问题标题】:HtmlElement.GetAttribute("for") Returns empty stringHtmlElement.GetAttribute("for") 返回空字符串
【发布时间】:2013-03-03 03:48:07
【问题描述】:

我正在使用 webbrowser 控件和 htmlElementcollection 类来获取表单中的所有 html。我让这个函数循环遍历所有内容,如果找到标签,它必须保存“For”属性中的值和标签的文本值。然后当找到一个输入元素时,它必须检查它的名称是否包含保存在变量 ForID 中的文本,如果是,则追加并显示。

代码:

private string logLabelInputs(HtmlElementCollection collection) {
        StringBuilder sb = new StringBuilder();
        var forID = "";
        var labelValue = "";
        foreach (HtmlElement item in collection) {
            if (item.TagName.ToLower() == "label") {
                forID = item.GetAttribute("for");
                forID = forID == "" ? item.GetAttribute("FOR") : forID;
                labelValue = item.InnerText;
            }
            if (item.TagName.ToLower() == "input") {
                if(item.Name.Contains(forID)) {
                    sb.Append(labelValue + ": " + item.Name + "\r\n\r\n");
                }
            }
            if (item.All.Count > 0) {
                sb.Append(logLabelInputs(item.All));
            }               
        }
        return sb.ToString();
    }

private void button4_Click(object sender, EventArgs e) {
        HtmlElementCollection tagsCollection = webBrowser1.Document.GetElementsByTagName("form");
        textBox1.Text = logLabelInputs(tagsCollection);
    }

所以发生的事情是 forID 始终为空。该值已正确保存。我检查大写和小写(我知道它是小写的)

有人对此有任何想法吗?

【问题讨论】:

    标签: c# html .net


    【解决方案1】:

    使用这个:

    item.GetAttribute("htmlfor") 
    

    【讨论】:

      【解决方案2】:

      最好使用jQueryPrototype 之类的任何javascript 框架。 这个 javascript 将是跨浏览器的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-27
        • 2020-07-11
        • 2015-05-25
        • 2011-05-09
        • 1970-01-01
        • 2012-07-20
        • 2011-12-15
        • 2020-11-23
        相关资源
        最近更新 更多