DOM 无法从 GWT 本机函数调用的 javascript 访问元素

DOM Element not accessible from javascript called from GWT native function

我想从 GWT 调用一个 javascript 函数,它接受由 UiBinder 创建的元素的元素 ID。函数失败,因为它找不到该元素。经过一些调试后,我发现了这个奇怪的行为:

log.info(DOM.getElementById("MyId").getAttribute("id"));
ScriptInjector.fromString("console.log(document.getElementById(\"MyId\"))").inject();
checkMyId();
log.info(DOM.getElementById("MyId").getAttribute("id"));

结果:

MyId
null
null
MyId

checkId 定义为:

public static native void checkMyId()/*-{
    console.log(document.getElementById("MyId"));
}-*/;

DOM#getElementById 相比,ScriptInjector 和本机函数似乎都对元素的访问受到限制。这种行为的原因是什么?如何调用 javascript 函数以便它可以访问该元素?

我试过使用 gwt 2.8.0-beta1 和 2.8.0-rc1,结果相同。

GWT JSNI Basics

When accessing the browser’s window and document objects from JSNI, you must reference them as $wnd and $doc, respectively. Your compiled script runs in a nested frame, and $wnd and $doc are automatically initialized to correctly refer to the host page’s window and document.