在 GWT jsni 中使用 jquery [英] Use jquery inside GWT jsni

查看:14
本文介绍了在 GWT jsni 中使用 jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用

$("#"+profileId).offset().top

从我的 gwt jsni 函数内部.我试过这个

from inside my gwt jsni function. I tried this

$wnd.$("#"+profileId).offset().top

但这也不起作用.我觉得我在这里缺少语法.有人可以帮忙吗

but this is also not working. I feel i am missing syntax here. Could anybody help

推荐答案

此问题的三个解决方案:

Three solutions for this question:

1- 您的 Jsni 代码看起来不错,只是您必须将其包含在相应的本机函数中并返回一个 double(如果您希望 gwt 进行转换,则返回任何其他数字类型).

1- Your Jsni code looks fine except that you have to enclose it in the corresponding native function and return a double (or any other number type if you want gwt to make the casting).

 native double getTop(String profileId) /*-{
   return $wnd.$("#" + profileId).offset().top;
 }-*/;

如果您想通过 UncaughExceptionHandler 查看错误,您应该将代码包装在 $entry 块中

If you wanted to see errors through your UncaughExceptionHandler you should wrap your code in a $entry block

native  double getTop(String profileId) /*-{
  return $entry(function(data) {
    return $wnd.$("#" + profileId).offset().top;
  });
}-*/;

2- 但是,我鼓励您使用 gwt-query 而不是使用 jQuery又名 gQuery.因此,您不必在 .html 中加载 jQuery,也无需在应用中处理 jsni.

2- But, instead of using jQuery, I encourage you to use gwt-query aka gQuery. So you dont have to load jQuery in your .html and yo dont need to deal with jsni in your app.

使用 gQquery,你有几乎相同的 jQuery 语法,但在 Java 中,所以你有类型安全、重构、测试......而且,你将有几十个实用程序(最简单的 ajax、promise、选择器等),它们是不在 gwt 核心中.

With gQquery you have almost the same jQuery syntax but in java, so you have type safe, refactoring, testing .... But also, you will have dozens of utilities (simplest ajax, promises, selectors, etc) which are not in the gwt core.

gQuery 是一个轻量级的库,用 gwt 从头开始​​完全重写.它不是 jQuery 库的包装器(就像在另一个答案中错误地说的那样),您不必在页面中包含 jquery.js.

gQuery is a light-weight library, fully rewritten from scratch in gwt. It is NOT a wrapper of the jQuery library (like is incorrectly said in the other answer), you dont have to include jquery.js in your pages.

与任何其他 gwt 库一样,gwt 编译器会删除您不从 gquery 使用的任何内容.在您的方法中,您的应用程序必须加载所有 jquery 内容.

Like with any other gwt library, the gwt compiler would get rid of anything you dont use from gquery. In your approach, your app has to load all the jquery stuff.

因此,在您的情况下,并使用 gquery 在您的 .java 类中编写此代码:

So in your case, and using gquery write this code in your .java classes:

import static com.google.gwt.query.client.GQuery.*;
... onModuleLoad() {
   int top = $("#"+profileId).offset().top;
}

3- 最后,如果您知道元素的 id,您可以选择使用纯 gwt 代码来获取元素的偏移量:

3- Finally, you have the option of using pure gwt code to get the offset of an element, if you know its id:

int top = DOM.getElementById(profileId).getOffsetTop();

这篇关于在 GWT jsni 中使用 jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆