lElementCollection和setAttribute函

lElementCollection和setAttribute函

本文介绍了htmlElementCollection和setAttribute函数不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的伙计们,

使用setAttribute函数为标签设置值时,我面临巨大挑战.
我已经创建了webrowser,然后加载了一个html页面init.然后我使用HtmlElementCollection收集了该页面的元素,然后当我尝试使用setAttribute设置特定元素的值时,选择了空白,这是无效的.下拉框仅适用于文本框,其工作正常.这是代码

Dear Guys,

I am facing a chalenge in setting a value for a tagelment using setAttribute function.
I had created webrowser,then i load an html page init.Then i collect the elments of that page using HtmlElementCollection then when i try to set the value for a particular elment using setAttribute its not workin a blank is selected.This is happening for drop down box elemnts only for text boxes its working fine.Here is the code

void SetCombo(string attribute, string attName, string value)
        {
            HtmlElementCollection tagsCollection = webBrowser1.Document.GetElementsByTagName("select");
            foreach (HtmlElement currentTag in tagsCollection)
            {
                if (currentTag.GetAttribute(attribute).Equals(attName))
                    currentTag.SetAttribute("value", value);

            }
        }



请帮助我



Please help me

推荐答案

currentTag.SetAttribute("selectedIndex", "4"); 



您还可以通过子选项控件进行解析,并将选定的属性设置为选定".



You can also parse through the child option controls and set the attribute selected as "selected"

Please find the code snippet.
 HtmlElementCollection tagsCollection = webBrowser1.Document.GetElementsByTagName("select");
            foreach (HtmlElement currentTag in tagsCollection)
            {
                if (currentTag.GetAttribute(attribute).Equals(attName))
                {
                    HtmlElementCollection optionCollection = currentTag.Document.GetElementsByTagName("option");
                    foreach (HtmlElement optionTag in optionCollection)
                    {
                        if (optionTag.GetAttribute("value").Equals("Thevalue you want to check"))
                            optionTag.SetAttribute("selected", "selected");

                    }
                }

            }


这篇关于htmlElementCollection和setAttribute函数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 22:53