现在的位置: 首页 > 综合 > 正文

C# WebBrowser执行自定义JS方法

2016年12月03日 ⁄ 综合 ⁄ 共 580字 ⁄ 字号 评论关闭
<span style="white-space:pre">	</span>/// <summary>
        /// 执行自定义JS函数
        /// </summary>
        /// <param name="Doc">要操作的WebBrowser的页面对象</param>
        /// <param name="JsFun">自定义JS函数</param>
        /// <param name="FunNanme">自定义JS函数名</param>
        /// <returns></returns>
        public string ExecJS(HtmlDocument Doc,string JsFun,string FunNanme) 
        {
            HtmlElement ele = Doc.CreateElement("script");
            ele.SetAttribute("type", "text/javascript");
            ele.SetAttribute("text", JsFun);
            Doc.Body.AppendChild(ele);
            return Doc.InvokeScript(FunNanme).ToString();
        }

DEMO:

HtmlDocument doc = web.Document;
ExecJS(doc, "function sayHello(){ return $('.signmiddle').text()}", "sayHello");

抱歉!评论已关闭.