在我的IE BHO中,我使用以下命令创建输入按钮元素:
var button = doc.createElement("input");
button.setAttribute("value", "myButton"); //next line gets an error
button.addEventListener("click", openFunction, false); //openFunction is a function inside of the same class
当我尝试调用button.addEventListener时,出现“ mshtml.IHTMLElement”不包含“ addEventListener”错误的定义。我觉得这很奇怪,因为根据此站点(http://help.dottoro.com/ljeuqqoq.php),我应该保持清醒状态。
我也看到了this线程,但是对于我想做的事情似乎有点过头了,但我无法使其正常工作。
最佳答案
终于让它工作了。必须对原始问题中链接的线程使用类似的方法。
/// Inside the BHO.cs file and in my namespace
/// Generic HTML DOM Event method handler.
///
public delegate void DHTMLEvent(IHTMLEventObj e);
///
/// Generic Event handler for HTML DOM objects.
/// Handles a basic event object which receives an IHTMLEventObj which
/// applies to all document events raised.
///
public class DHTMLEventHandler
{
public DHTMLEvent Handler;
HTMLDocument Document;
public DHTMLEventHandler(HTMLDocument doc)
{
this.Document = doc;
}
[DispId(0)]
public void Call()
{
Handler(Document.parentWindow.@event);
}
}
public void BrowserEventHandler(IHTMLEventObj e)
{
try
{
if (e.type == "click" && e.srcElement.id == "IDOfmyButton")
{
// do something.
something();
}
}
catch
{
}
}
在我的OnDocumentComplete方法内(也位于与上述相同的名称空间中[新手的额外信息]):
DHTMLEventHandler myHandler = new DHTMLEventHandler(framedoc);
myHandler.Handler += new DHTMLEvent(this.BrowserEventHandler);
button.onclick = myHandler;
要做很多工作只是为了单击一个按钮。在Firefox中,这是一行:O