gpt4 book ai didi

android - JQuery 在 WebView 上不起作用

转载 作者:IT老高 更新时间:2023-10-28 23:23:24 25 4
gpt4 key购买 nike

我尝试在 Assets /脚本和 Internet 上包含 JQuery 文件,但没有显示警告对话框。我得到了日志输出并将其设置为 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>

最佳答案

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

改成

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

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

关于android - JQuery 在 WebView 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6751256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com