我有[object HTMLIFrameElement],我想获取它的文档。我宁愿不使用本机方法。这是我现在尝试的,但不起作用。
//This is iFrame container with iFrame
mainWidge = new MainWidgetViewImpl(base);
//This is HTMLIFrameElement
Element iframeElement = mainWidge.getElement().getFirstChildElement();
//I've tried this, but it gets null pointer exception
IFrameElement iframe = (IFrameElement)mainWidge.getElement().getFirstChildElement();
//Here is: "Cannot read property 'document' of null"
Document doc = iframe.getContentDocument();
这意味着iframe的contentWindow
是null
,如果iframe没有附加到文档中,就会出现这种情况。
contentWindow
IDL属性必须返回iframe
元素的嵌套浏览上下文的WindowProxy
对象(如果有的话),否则为null。
--来源:https://html.spec.whatwg.org/multipage/embedded-content.html#dom-iframe内容窗口
当
iframe
元素插入到具有浏览上下文的文档中时,用户代理必须创建嵌套的浏览上下文,然后"第一次"处理iframe
属性。从文档中删除
iframe
元素时,用户代理必须丢弃嵌套的浏览上下文(如果有的话)。
--来源:https://html.spec.whatwg.org/multipage/embedded-content.html#the-iframe元素