问题描述
有人说可以,但我没有看到任何的JQuery例中的WebView。我试了很多办法,但没有成功。
这是一个简单的例子code。 CSS的工作,但JQuery的没有。
加入 1)Internet权限。
2)中启用JavaScript。
3)测试仿真器上的1.6和2.2。
公共类UsingJQueryActivity延伸活动{
私人的WebView web视图; / **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(最终捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main); web视图=(的WebView)findViewById(R.id.webView1);
webView.getSettings()setJavaScriptEnabled(真)。
webView.loadUrl(文件:///android_asset/js_please_run.html);
}
}
资产/ js_please_run.html
< HTML和GT;
< HEAD>
<脚本SRC =http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.js类型=文/ JavaScript的>< / SCRIPT>
<脚本SRC =https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js类型=文/ JavaScript的>< / SCRIPT>
<脚本>
警报(世界,你好');
确认(你好Android的);
< / SCRIPT>
<风格>
DIV {
颜色:红色;
}
< /风格>
< /头>
<身体GT;
< DIV>
我所听到的是raindrops.Falling在屋顶上。哦,宝贝,告诉我为什么你必须去。
造成这种痛苦我觉得你会不会消失。而今天,我正式想念你。
< / DIV>
< /身体GT;
< / HTML>
的WebView
将默认不显示对话框,因为这是用户界面的一部分,你必须在一些其他的方式测试JavaScript或者设置一个 WebChromeClient
显示对话框,请参阅
这是有帮助的设置 WebChromeClient
像这样的:
公共类DebugWebChromeClient扩展WebChromeClient {
私有静态最后弦乐TAG =DebugWebChromeClient; @覆盖
公共布尔onConsoleMessage(ConsoleMessage米){
Log.d(TAG,m.lineNumber()+:+ m.message()+ - + m.sourceId()); 返回true;
}}
Somebody say can, but I don't see any example about JQuery in WebView. I tried many ways but no succeed.This is a simple example code. The CSS works but JQuery doesn't.
1) Internet permission was added.
2) Javascript was enabled.
3) Tested on emulator 1.6 and 2.2.
public class UsingJQueryActivity extends Activity {
private WebView webView;
/** Called when the activity is first created. */
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView = (WebView)findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/js_please_run.html");
}
}
"assets/js_please_run.html"
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
<script>
alert('Hello world');
confirm("Hello android");
</script>
<style>
div {
color: red;
}
</style>
</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
will by default not show dialogs as that is part of the UI, you'll have to test javascript in some other way or set a WebChromeClient
that shows dialogs, see onJsAlert()
It is helpful to set a WebChromeClient
like this one:
public class DebugWebChromeClient extends WebChromeClient {
private static final String TAG = "DebugWebChromeClient";
@Override
public boolean onConsoleMessage(ConsoleMessage m) {
Log.d(TAG, m.lineNumber() + ": " + m.message() + " - " + m.sourceId());
return true;
}
}
这篇关于是否JQuery的真正的WebView工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!