代码之家  ›  专栏  ›  技术社区  ›  Greg

不能设置iHTMLVEndoB2::FROM元素

  •  33
  • Greg  · 技术社区  · 17 年前

    我正试图在InternetExplorer扩展中生成合成的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
    

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

    2 回复  |  直到 14 年前
        1
  •  1
  •   Luke Baughan    13 年前

    只是黑暗中的一个野生镜头,但这可能是因为您将一个“null”对象传递给createEventObject方法吗?如果你改变这个呢:

    Object o = null;
    

    对此:

    Object o = new Object();
    

    在你例子的第3行?

        2
  •  1
  •   outcoldman    13 年前

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