开发者

Calling JavaScript in a WebBrowser control from C#

For example: 1) In webBrowser1 page index.html is loaded. 2) This page has the following code:

...
<a id="activity_text" href="#" onclick="activity_editor.开发者_运维百科show();return false;">now status</a>
...

3) As I can in the program way to change "now status"?

I tried so:

HtmlElement collH1 = document.GetElementById("activity_text");
collH1.InnerText = "new status";

But this way works only in the control webBrowser1. If then to come to look through IE/Opera/FF that has varied of nothing...


If you are using System.Windows.Forms.WebBrowser:

HtmlElement collH1 = document.GetElementById("activity_text");
object obj = collH1.DomElement;
System.Reflection.MethodInfo mi = obj.GetType().GetMethod("click");
mi.Invoke(obj, new object[0]);

If you're using mshtml:

HtmlElement collH1 = document.GetElementById("activity_text");
mshtml.HTMLAnchorElement el2 = (mshtml. HTMLAnchorElement)collH1.DomElement;
el2.click();

This will execute a click similar to a user clicking the link in a browser, which I guess is what you want to accomplish(?).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜