我正在尝试在我的 Android 应用程序的 WebView 中加载本地 HTML 文件。 HTML 文件引用了 jquerymobile.js
的本地副本。我的问题
是应用程序启动时,抛出以下错误:
TypeError 'undefined' is not an object (evaluating $ 事件)
即使没有编写任何使用 jquery lib
的代码,也会发生这种情况。当我从他们的 CDN
加载 jquery 库时,不会发生此错误。
关于我做错了什么有什么想法吗? 感谢您的宝贵时间。
为更清晰添加代码:
这是 WebView
加载 url
try {
//this reads the jquerymobile as a string
jmobile = readFileAsString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
wview1.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
//Required functionality here
return super.onJsAlert(view, url, message, result);
}
});
wview1.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(wview1, url);
wview1.loadUrl("javascript:"+jmobile);
// do your stuff here
}
});
wview1.loadUrl("file:///android_asset/html/index.html");
以下作品
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.css" />
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>
但是如果我将脚本 src 更改为 jquery.mobile-1.1.1.min.js 的本地副本,我会收到类型错误
最佳答案
当您引用本地文件时,请按以下顺序放置这些引用:
- 移动 CSS
- jQuery核心JS
- jQuery 移动版 JS。
希望对您有所帮助。
关于android - TypeError 'undefined' 不是对象(评估 $ 事件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12249575/