Javascript本机方法引发意外异常 [英] Javascript native method throws unexpectedly exception
问题描述
我正在将vaadin项目从 vaadin 6
版本更新为 vaadin 7
。它包含一些在旧版本中很好的原生Javascript方法。但是有些人在被调用时会抛出异常。我不熟悉 Javascript
编程,因此我无法识别错误的来源。特别的方法是:
I am updating a vaadin project from vaadin 6
version to vaadin 7
. It contains some native Javascript methods which work well in the old version. However some of them throw an exception when they should be called. I am not acquainted enough to Javascript
programming and therefore I am not able to identify the source of the error. In particular the method is:
protected static native void addSthToImg()
/*-{
$wnd.$(document).ready(function() {
var = $wnd.$;
var('.settingswrapper > img').addClass("imgStartup");
if (var('.settingswrapper > img').hasClass("imgStartup")) {
console.log("imgStartup exist");
var('.settingswrapper > img')
.animate(null, 300, function() {
var('.settingswrapper > img').removeClass("imgStartup");
var('.settingswrapper > img').addClass("imagePopIn");
});
}
});
}-*/;
在Chrome的控制台中,我看到错误堆栈跟踪:
On the Console in Chrome I see the error stack trace:
Caused by: com.google.gwt.core.client.JavaScriptException: (TypeError) : Object [object global] has no method '$'
什么可能是错误的可能原因,为什么它在旧版本中工作?一个jar /库缺少?
What could be a possible cause of the error and why is it working in the old version? Is a jar/library missing?
推荐答案
看起来像jQuery别名 $
没有在全局范围中定义(由 $ wnd
表示)。你确定你的页面包含jQuery?
Seems like jQuery alias $
is not defined in the global scope (denoted by $wnd
). Are you sure your page includes jQuery?
你应该有一些类似< script src =/ js / jquery.jstype =文本/ javascript>< / script>
(但指向项目中jquery.js的实际位置)。我不知道Vaadin是否自动管理JavaScript库,因为我不熟悉框架。
You should have something like <script src="/js/jquery.js" type="text/javascript"></script>
(but pointing to the actual location of jquery.js in your project) in your HTML source. I don't know if Vaadin manages JavaScript libraries automatically, since I'm not familiar with the framework.
这篇关于Javascript本机方法引发意外异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!