简体   繁体   English

如何防止GWT中的按钮单击事件重新加载html代码

[英]how to prevent html code reload on button click event in GWT

I created a button in GWT and on button click event i tried to get the text of div in alert box. 我在GWT中创建了一个按钮,并在按钮单击事件上尝试在警报框中获取div的文本。 so i did the following 所以我做了以下

Button btnClick = new Button("Click Me");
getElement().setInnerHTML("<div id='dummy'>This is dummy Text</div>");
btnClick.addClickHandler(new ClickHandler() {
    public void onClick(ClickEvent event) {
        // handle the click event
        Window.alert(getImageStr());
    }
});
RootPanel.get("dummy").add(btnClick);

And this is the method declaration: 这是方法声明:

private native String getDummyText() /*-{
        return document.getElementById("dummy").innerText;
}-*/;

But when i try to run it. 但是当我尝试运行它时。 it throws an exception 引发异常

TypeError: Cannot read property 'innerText' of null

and when i saw in inspect element while stopping at break point in javascript, i was shocked to see many scripts in body tag and no HTML at that moment and when i release that break point, it created HTML again. 当我在javascript的断点处停止时在检阅元素中看到的时候,我很震惊地看到body标记中有许多脚本,并且那时没有HTML,当我释放该断点时,它又创建了HTML。 Please anyone help me out. 请任何人帮我。 i am badly stuck in that. 我深陷其中。 Thank You 谢谢

You need to replace document with $doc in JSNI code: 您需要使用JSNI代码中的$doc替换document

When accessing the browser's window and document objects from JSNI, you must reference them as $wnd and $doc, respectively. 从JSNI访问浏览器的窗口和文档对象时,必须分别将它们引用为$ wnd和$ doc。 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. 您的编译脚本在嵌套框架中运行,并且$ wnd和$ doc会自动初始化以正确引用宿主页面的窗口和文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM