JQuery在WebView上无法使用

17

我试图在assets/scripts中包含JQuery文件,并在互联网上寻找帮助,但弹出对话框没有显示出来。我获取了log输出并将其输出到output.html,在Windows中可以正常工作(太奇怪了!)。 WebView有什么问题吗?

public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    webView = (WebView) findViewById(R.id.webView);
    final String s = "<html><head>" +
    "<link href=\"css/my.css\" type=\"text/css\" rel=\"stylesheet\" />" +
    "<script src=\"scripts/jquery-1.6.2.min.js\" rel=\"stylesheet\" type=\"text/javascript\"></script>" +
    "<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js\" type=\"text/javascript\"></script>" +
    "<script>" +
    "$(document).ready(function(){ alert('hello'); });" +
    "</script>" +
    "</head><body><div>All I hear is raindrops." +
    "Falling on the rooftop. Oh baby tell me why you have to go. " +
    "Cause this pain I feel you won't go away. And today, " +
    "I'm officially missing you.</div></body></html>";
    webView.getSettings().setJavaScriptEnabled(true);
    Log.d("Something", s);
    webView.loadDataWithBaseURL("file:///android_asset/", s, "text/html", "utf-8", null);
}

在添加扩展名“.html”后,这是日志输出。它可以在Firefox上工作,但在WebView上无法工作。:(

<html>
<head>
<link href="css/my.css" type="text/css" rel="stylesheet" />
<script src="scripts/jquery-1.6.2.min.js" rel="stylesheet" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js" type="text/javascript"></script>
<script>$(document).ready(function(){ alert('hello'); });</script>
</head>
<body>
    <div>
    All I hear is raindrops.Falling on the rooftop. Oh baby tell me why you have to go. Cause this pain I feel you won't go away. And today, I'm officially missing you.
    </div>
</body>
</html>

你解决了吗?我现在也遇到了这个问题。我该怎么办?:( - Kaidul
6个回答

7
webView.loadDataWithBaseURL("file:///android_asset/", s, "text/html", "utf-8", null);

变更为

webView.loadDataWithBaseURL(getAssets(), s, "text/html", "utf-8", null);

要获取资源文件,您需要访问应用程序的资源路径。在Android上,应用程序是用户,因此无法访问以“file://”开头的目录。


2
错误: loadDataWithBaseUrl()中的getAssets参数错误。 - Cyph3rCod3r

2
我猜,一些像prompt和alert(系统弹出窗口)的javascript函数应该由你的代码实现。 一个简单的指南...
1. 创建一个名为Jscalls.java的类。
public class Jscalls {

    Context mContext;
    Jscalls(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */
    public void alert(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }    
}

2. 在你的程序中添加webView.addJavascriptInterface(new Jscalls(this), "Android");

3. 在 HTML 页面中Android.alert("hello") 代替 alert("hello")

希望它能正常工作 :)


是的,当然可以。不过我们似乎面临着完全不同的问题。 - Darpan

2

如已在Android WebView doesn't load jQuery中提到:

你的scripts/jquery-1.6.2.min.js脚本位于何处?如果它位于您的assets目录中,则应该以assets目录作为baseUrl初始化webView:

webView.loadDataWithBaseURL("file:///android_asset/", data, "text/html", "UTF-8", null);

或者

webView.loadUrl("file:///android_asset/file.html");

你可以尝试创建一个简单的 .js 文件,其中包含一个简单的函数,例如:
function dummy(document) { document.write("Hooray it works"); }

尝试访问你的html中的dummy函数来测试.js文件是否已包含。


2

为使此功能正常工作,您需要在 assets/scripts 文件夹中拥有 jquery.js 文件。

scripts/jquery-1.6.2.min.js

这样就可以了。


我刚刚使用完全相同的代码进行了检查,它可以正常工作。你确定你用正确的文件名保存了它吗?或者如果你想从网络上获取它,尝试添加INTERNET权限,这也可以正常工作。 - Kumar Bibek
1
@Emerald214 对我也不起作用,我已经注释掉了 CSS 行,并且在根资产文件夹中有一个 jquery.min.js 文件,使用 Android 3.0 在一台 3.1 设备上。 - CQM

2
WebView设置中的setPluginsEnabled设置为true,即可启用插件。

1
你能提供一些示例代码吗?我在与WebView相关的任何地方都没有看到“setPluginsEnabled”函数。 - Michael

1

例如,您应该提供完整路径来加载JavaScript和CSS。

<link href="http://yourdomain.com/css/my.css" type="text/css" rel="stylesheet" />

2
我已经做了那个,问题可能出在其他地方。感谢您的建议。 - Darpan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接