C#javascript按标签和属性点击按钮

时间:2014-10-11 23:03:31

标签: javascript c# jquery

我在点击按钮时遇到了一些问题:

 <a class="button_recruit" style="letter-spacing: -1px" href="/myrecruitlink" data-executing="0">Recruit</a>      

这是我试图点击的按钮,但当我为它添加代码时,它什么也没做,我是C#的新手,并试图学习这个。所以,如果有人能指出我在这里做错了什么,我会非常感激。

以下是程序代码:

 namespace clean_test
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        web.ScriptErrorsSuppressed = true;
        Registry.SetValue("HKEY_CURRENT_USER\\AppEvents\\Schemes\\Apps\\Explorer\\Navigating\\.Current", "", "NULL");
        web.Navigate("http://www.mywebsite.com");
    }

    private void recruit_Click(object sender, EventArgs e)
    {
        HtmlElementCollection elems = web.Document.GetElementsByTagName("a");
        foreach (HtmlElement elem in elems)
        {
            String value = elem.GetAttribute("value");
            if (value != null && value.Length != 0 && value.Equals("Recruit"))
            {
                recruit.PerformClick();
            }
        }
    }


  }
}    

2 个答案:

答案 0 :(得分:0)

为什么不去客户方?

<script type="text/javascript">
function NewFunction(){
//do something here
}
</script>

并切换到OnClientClick属性

的按钮
<asp:Button ID="Button1" runat="server" Text="Button" CssClass="button_recruit" style="letter-spacing: -1px"  OnClientClick="recruit_Click"  />

或者将onclick添加到您必须触发javascript方法的现有元素。

 <a class="button_recruit" style="letter-spacing: -1px" href="/myrecruitlink" data-executing="0" onclick=""NewFunction();">Recruit</a>   

答案 1 :(得分:0)

我认为其他人不理解你的问题。你需要这一行:

private void recruit_Click(object sender, EventArgs e)
{
     HtmlElementCollection elems = web.Document.GetElementsByTagName("a");
     foreach (HtmlElement elem in elems)
     {
         String value = elem.GetAttribute("value");

         //you can use elem.InnerText.Equals("Recruit") too, if value == null.
         if (value != null && value.Length != 0 && value.Equals("Recruit"))
         {
             elem.InvokeMember("click");
         }
     }
 }

希望我帮助你!