简体   繁体   English

从C#在WebBrowser控件中调用JavaScript

[英]Calling JavaScript in a WebBrowser control from C#

For example: 1) In webBrowser1 page index.html is loaded. 例如:1)在webBrowser1页面中加载index.html。 2) This page has the following code: 2)该页面具有以下代码:

...
<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"? 3)是否可以通过程序方式更改“现在状态”?

I tried so: 我是这样尝试的:

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

But this way works only in the control webBrowser1. 但是这种方式仅适用于控件webBrowser1。 If then to come to look through IE/Opera/FF that has varied of nothing... 如果要看一看IE / Opera / FF的话,什么也没变...

If you are using System.Windows.Forms.WebBrowser: 如果您正在使用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: 如果您使用的是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(?). 这将执行与用户单击浏览器中的链接类似的单击,我想这就是您想要完成的操作(?)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM