jQuery 和 Java 小程序 [英] jQuery and Java applets
问题描述
我正在开发一个项目,其中我们将 Java 小程序用于部分 UI(特别是地图),但在 HTML/JavaScript 中围绕小程序构建 UI 的其余部分,通过与小程序通信LiveConnect/NPAPI.我知道这有点奇怪,但让我们假设设置不在讨论中.我开始计划使用 jQuery 作为我的 JavaScript 框架,但我遇到了两个问题.
发出第一个:
选择小程序并不提供对小程序方法的访问.
Java:
public class MyApplet extends JApplet {//...公共字符串 foo() { return "foo!";}}
JavaScript:
var applet = $("#applet-id");警报(applet.foo());
运行上面的 JavaScript 结果
$("#applet-id").foo 不是函数
这与 Prototype 形成对比,Prototype 中类似的代码确实有效:
var applet = $("applet-id");警报(applet.foo());
那么……applet 方法去哪儿了?
发出第二个:
Firefox 2 中的 jQuery 和小程序存在一个已知问题:http://www.pengoworks.com/workshop/jquery/bug_applet/jquery_applet_bug.htm
这是一个远景,但有人知道解决方法吗?我怀疑这个问题无法修复,这意味着要切换到 Prototype.
感谢您的帮助!
第一个问题,试试怎么样
alert( $("#applet-id")[0].foo() );
这里的第二个问题是线程可能的解决方法.
引用解决方法
<块引用>//防止IE内存泄漏//并防止在其他浏览器中使用鼠标悬停等事件刷新时出错//不包括窗口,以免解除现有卸载事件的绑定jQuery(window).bind("卸载",功能() {jQuery("*").add(document).unbind();});
将该代码更改为:
<块引用>//不包含窗口,以免解除现有卸载事件的绑定jQuery(window).bind("卸载",功能() {jQuery("*:not('applet, object')").add(document).unbind();});
I'm working on a project where we're using a Java applet for part of the UI (a map, specifically), but building the rest of the UI around the applet in HTML/JavaScript, communicating with the applet through LiveConnect/NPAPI. A little bizarre, I know, but let's presume that setup is not under discussion. I started out planning on using jQuery as my JavaScript framework, but I've run into two issues.
Issue the first:
Selecting the applet doesn't provide access to the applet's methods.
Java:
public class MyApplet extends JApplet {
// ...
public String foo() { return "foo!"; }
}
JavaScript:
var applet = $("#applet-id");
alert(applet.foo());
Running the above JavaScript results in
$("#applet-id").foo is not a function
This is in contrast to Prototype, where the analogous code does work:
var applet = $("applet-id");
alert(applet.foo());
So...where'd the applet methods go?
Issue the second:
There's a known problem with jQuery and applets in Firefox 2: http://www.pengoworks.com/workshop/jquery/bug_applet/jquery_applet_bug.htm
It's a long shot, but does anybody know of a workaround? I suspect this problem isn't fixable, which will mean switching to Prototype.
Thanks for the help!
For the first issue, how about trying
alert( $("#applet-id")[0].foo() );
For the second issue here is a thread with a possible workaround.
Quoting the workaround
// 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(); });
change that code to:
// Window isn't included so as not to unbind existing unload events jQuery(window).bind("unload", function() { jQuery("*:not('applet, object')").add(document).unbind(); });
这篇关于jQuery 和 Java 小程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!