jQuery和Java小程序

时间:2020-03-05 18:51:38  来源:igfitidea点击:

我正在做一个项目,其中我们将Java applet用于部分UI(特别是地图),但是在HTML / JavaScript中围绕applet构建其余的UI,并通过LiveConnect / NPAPI与applet通信。我知道有些奇怪,但让我们假定设置尚未在讨论中。我开始计划使用jQuery作为JavaScript框架,但是遇到了两个问题。

发出第一个:

选择applet并不能访问applet的方法。

Java的:

public class MyApplet extends JApplet {
  // ...
  public String foo() { return "foo!"; }
}

JavaScript:

var applet = $("#applet-id");
alert(applet.foo());

运行上面的JavaScript会导致

$("#applet-id").foo is not a function

这与Prototype相反,在Prototype中,类似的代码可以工作:

var applet = $("applet-id");
alert(applet.foo());

那么...小程序方法去哪里了?

发出第二个:

在Firefox 2中,jQuery和小程序存在一个已知问题:http://www.pengoworks.com/workshop/jquery/bug_applet/jquery_applet_bug.htm

这是一个很长的路要走,但是有人知道解决方法吗?我怀疑这个问题无法解决,这意味着要切换到原型。

谢谢帮助!

解决方案

回答

对于第一个问题,如何尝试

alert( $("#applet-id")[0].foo() );

对于第二个问题,这里是一个具有可能的解决方法的线程。

引用解决方法

// Prevent memory leaks in IE
// And  prevent errors on refresh with events  like mouseover in other  browsers
// Window isn't included so as not to unbind existing unload events
jQuery(window).bind("unload",
function() {
        jQuery("*").add(document).unbind();
});

将该代码更改为:

// Window isn't included so as not to unbind existing unload events
jQuery(window).bind("unload",
function() {
        jQuery("*:not('applet, object')").add(document).unbind();
});