JQuery不适用于WebView

eme*_*ieu 17 jquery android android-webview android-2.2-froyo

我试图将JQuery文件包含在资产/脚本和Internet上,但是警告对话框没有显示.我得到了日志输出并使其成为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);
}
Run Code Online (Sandbox Code Playgroud)

这是添加扩展名".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>
Run Code Online (Sandbox Code Playgroud)

小智 7

webView.loadDataWithBaseURL("file:///android_asset/", s, "text/html", "utf-8", null);
Run Code Online (Sandbox Code Playgroud)

改变

webView.loadDataWithBaseURL(getAssets(), s, "text/html", "utf-8", null);
Run Code Online (Sandbox Code Playgroud)

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

  • 错误:),loadDataWithBaseUrl()中错误的参数getAssets; (2认同)

Kum*_*bek 2

您需要在 asset/scripts 文件夹中包含 jquery.js 文件才能使其正常工作。

脚本/jquery-1.6.2.min.js

那应该有效。