我是一个 GWT 人员,jquery 经验为零。对于那个很抱歉。不幸的是,我遇到了一些必须在 GWT 项目中使用的 jquery 功能。
<script type="text/javascript">
$(document).ready(function() {
zingchart.render({
'id' : 'g1',
'width' : 500,
'height' : 400,
'dataurl' : 'scatter_minimal.txt'
});
});
</script>
<div class="g" id="g1"></div>
以我粗糙的直觉,我即将相信这一点
$(document).ready( ..)
应翻译为 GWT 的 onModuleLoad(){ ....}
,其中 onModuleLoad 将确保 DOM 已准备就绪(如果我在 onModuleLoad 中调用该函数)。
但我认为以下内容无效..
private static native void render() /*-{
function() {
zingchart.render(
{
'id' : 'g1',
'width' : 500,
'height' : 400,
'dataurl' : 'scatter_minimal.txt'
}
);
}
}-*/;
如何编写 JSNI 代码来定义可以从 GWT 调用的函数?
最佳答案
如果我正确理解你的问题,你想做的就是:
private static native void render() /*-{
zingchart.render(
{
'id' : 'g1',
'width' : 500,
'height' : 400,
'dataurl' : 'scatter_minimal.txt'
}
);
}-*/;
然后您可以从 GWT 代码中调用 render()
。 render
是函数本身,当您使用 JSNI 语法时,GWT 已经将其定义为 JavaScript 函数。
背景:
编写 function() {...}
定义一个匿名函数 - 这不是您想要的(您没有任何方式引用它,因为您没有通过它在任何地方)。在 jQuery 中,您可以将该匿名函数直接传递给 document.ready()
。
关于java - 从 GWT JSNI 调用 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7236435/