1

我用javascript注入了一个新的输入控件

 element = axWebBrowser.Document.CreateElement("input");
                element.SetAttribute("id", "inpinp");
                element.SetAttribute("type", "button");
                element.SetAttribute("value", "Hello World");
                element.SetAttribute("onclick", "javascript: alert('hello');");

它在浏览器中显示控件,但是当我单击无操作时。

我在这里错过了什么吗?

4

1 回答 1

0

这会注入一个按钮,然后为 click 事件设置一个不显眼的事件侦听器:

HtmlElement myElem = webBrowser1.Document.CreateElement("input");
HtmlElement script = webBrowser1.Document.CreateElement("script");

dynamic element = myElem.DomElement;
element.SetAttribute("value", "Hello, World!");
element.SetAttribute("type", "button");
element.SetAttribute("id", "myButton");

dynamic elemScript = script.DomElement;
elemScript.text = "function bindEvent() { var myButton = document.getElementById('myButton'); if(myButton.addEventListener){ myButton.addEventListener('click',clicked,false);}else if(myButton.attachEvent){myButton.attachEvent('onclick',clicked);}} function clicked(){alert('clicked');}";
((HtmlElement)webBrowser1.Document.GetElementsByTagName("body")[0]).AppendChild(myElem);
((HtmlElement)webBrowser1.Document.GetElementsByTagName("body")[0]).AppendChild(script);
webBrowser1.Document.InvokeScript("bindEvent");

祝你好运!

于 2013-03-20T21:27:29.600 回答