JQuery的不上的WebView工作 [英] JQuery doesn't work on WebView

查看:166
本文介绍了JQuery的不上的WebView工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想包括资产/脚本和互联网JQuery的文件,但警告对话框不显示。我得到的日志输出,使其output.html,它工作在Windows(太奇怪了!)。 做什么用的WebView问题?

 公共无效的onCreate(最终捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    web视图=(web视图)findViewById(R.id.webView);
    最终的String =< HTML>< HEAD>中+
    <链接HREF = \的CSS / my.css \型= \文/ CSS \相对= \样式\/>中+
    <脚本SRC = \脚本/ jQuery的-1.6.2.min.js \相对= \样式\类型= \文/ JavaScript的\>< / SCRIPT>中+
    <脚本SRC = \HTTPS://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js \型= \文/ JavaScript的\>< / SCRIPT>中+
    &其中;脚本>中+
    $(文件)。就绪(函数(){警报('你好');}); +
    &所述; /脚本>中+
    < /头><身体GT;< D​​IV>我所听到的是雨滴。 +
    落在屋顶。宝贝哦告诉我为什么你得走了。+
    因为这痛苦,我觉得你会不会消失。而今天,+
    我正式想念你< / DIV>< /身体GT;< / HTML>中;
    webView.getSettings()setJavaScriptEnabled(真)。
    Log.d(东西,S);
    webView.loadDataWithBaseURL(文件:/// android_asset /,S,text / html的,UTF-8,NULL);
}
 

这是添加扩展名.html后的日志输出。它适用于Firefox的,但不上的WebView。 :(

 < HTML>
< HEAD>
<链接HREF =CSS / my.css类型=文本/ CSS相对=样式/>
<脚本SRC =脚本/ jQuery的-1.6.2.min.js相对=样式类型=文/ JavaScript的>< / SCRIPT>
<脚本SRC =htt​​ps://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js类型=文/ JavaScript的>< / SCRIPT>
<脚本> $(文件)。就绪(函数(){警报('你好');});< / SCRIPT>
< /头>
<身体GT;
    < D​​IV>
    我所听到的是raindrops.Falling在屋顶上。哦,宝贝,告诉我为什么你必须去。导致这种痛苦,我觉得你不会消失。而今天,我正式想念你。
    < / DIV>
< /身体GT;
< / HTML>
 

解决方案

  webView.loadDataWithBaseURL(文件:/// android_asset /,S,text / html的,UTF -8,NULL);
 

是改变

  webView.loadDataWithBaseURL(getAssets(),S,text / html的,UTF-8,NULL);
 

要获得资源文件,您需要访问应用程序的资产路径。一个应用程序是Android上的用户,因此不能访问路径开头文件://。目录

I tried to include JQuery files in assets/scripts and on Internet, but the alert dialog doesn't show. I got the log output and make it output.html, it works in Windows (so strange!). What's the problem with 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);
}

This is the log output after adding extension ".html". It works on Firefox but does not on 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);

be change to

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

to get asset file, you will need to access app's asset path. An app is an user on Android, so cannot access path begin with "file://" directory.

这篇关于JQuery的不上的WebView工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆