33

我正在尝试在 Internet Explorer 扩展中生成合成 Javascript 事件,但我无法让 fromElement 属性保持不变。这是我的代码的摘录:

MsHtml.IHTMLDocument4 doc4 = ... // the document object
Object o = null;
MsHtml.IHTMLEventObj2 eObj = 
    (MsHtml.IHTMLEventObj2)doc4.CreateEventObject(ref o);

// string that specifies the from element, e.g. "document.getElementById('id1')":
string locator = ... 
object from = doc4.Script.GetType().InvokeMember("eval", 
                                                 BindingFlags.InvokeMethod, 
                                                 null, 
                                                 doc4.Script, 
                                                 new object[] { locator });

// from now holds a ref to an object that implements the IHTMLElement interface
eObj.fromElement = from;
IHTMLElement el = eObj.fromElement;
// el == null

我在这里做错了什么?eObj.fromElement 应该等于 from,但它似乎没有被设置。

4

2 回答 2

1

只是在黑暗中狂野射击,但可能是因为您将“空”对象传递给 CreateEventObject 方法吗?如果你改变这个怎么办:

Object o = null;

对此:

Object o = new Object();

在你的例子的第 3 行?

于 2012-08-09T09:48:57.860 回答
1

要找到正确的元素,您应该尝试 IHTMLDocument6 中的 getElementById 方法:http: //msdn.microsoft.com/en-us/library/cc288667

于 2012-08-13T15:21:40.090 回答