需求:为网页上个链接增加点击事件,但是这个链接无法增加js代码
url:http://public.rongcloud.cn/view/D4F444BE2D94D760329F3CF38B4AE35C
网页截图:
需要给“投融资讯“四个字设置点击事件
但是观察源码
<body> | |
<div class="wrap content"> | |
<div class="content-inner"> | |
<div class="content-header"> | |
<h2 class="content-header-title">发货房间很干净或</h2> | |
<div class="content-header-info"> | |
<span>2017-01-08</span> | |
<span class="userId"><ahref="javascript:;">投融资讯</a></span> | |
</div> | |
</div> | |
<div class="content-body" > | |
<p style="text-align: center;margin:10px 0;"> | |
<img style="" src="http://7xi6ox.com1.z0.glb.clouddn.com/6ccb1a46485ecbe01324e130564a0fd9?imageView/0/w/200/h/170" /> | |
</p> | |
<p style="margin-top:20px;" > | |
<a onclick="count('http://public.rongcloud.cn/view/11C329D37EFD51832EF7E3CF2A0CE2D4', 'D4F444BE2D94D760329F3CF38B4AE35C')"style="color:#607fa6;text-decoration:none;"href="javascript:void(0)" >阅读原文</a> | |
</p> | |
</div> | |
<div class="divider" style="height: 1px;margin: 9px 0;overflow: hidden;background-color: #e5e5e5;margin-top: 18px;"></div> | |
</div> |
并没有具体的onclick事件,怎么办呢?
1、首先在Chrome上写了端测试代码,测试没问题
</div><div> var child=document.getElementsByTagName('a')[0];</div><div> child.οnclick=function(){</div><div> userIdClick();</div><div> };</div><div> function userIdClick(){ </div><div> myObj.getClose();</div><div><br /></div><div><br /></div><div> };</div><div>
2、然后使用安卓端注入进来
webview.getSettings().setJavaScriptEnabled(true); webview.addJavascriptInterface(new JSObject(), "myObj"); webview.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url) { Toast.makeText(MainActivity.this, "wodo-7-", Toast.LENGTH_SHORT).show(); view.loadUrl(url); return true; } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); String js = "var script = document.createElement('script');"; js+= "script.type = 'text/javascript';"; js+="var child=document.getElementsByTagName('a')[0];"; js+="child.οnclick=function(){userIdClick();};"; js+= "function userIdClick(){myObj.getClose();};"; webview.loadUrl("javascript:" + js); } }); class JSObject { @JavascriptInterface // sdk17版本以上加上注解 public String getData(String txt) { return "12345678"; } @JavascriptInterface // sdk17版本以上加上注解 public void getClose() { Toast.makeText(MainActivity.this, "dododo", Toast.LENGTH_SHORT) .show(); // finish(); } }
这样就可以实现点击调用本地方法了。
二、如果想获取html中某个内容
@Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); String js = "var script = document.createElement('script');"; js+= "script.type = 'text/javascript';"; js+="var child=document.getElementsByTagName('a')[0];"; js+="child.οnclick=function(){userIdClick();};"; js+= "function userIdClick(){myObj.getClose();};"; webview.loadUrl("javascript:" + js); // view.loadUrl("javascript:window.myObj.showSource(document.getElementsByTagName('p')[1].innerHTML);"); view.loadUrl("javascript:myObj.showSource(document.getElementsByTagName('p')[0].innerHTML);"); //关键代码 } }); /** * 本地化JS对象(第一种方法) * * @author jiangwei1-g */ class JSObject { @JavascriptInterface // sdk17版本以上加上注解 public String getData(String txt) { return "12345678"; } @JavascriptInterface // sdk17版本以上加上注解 public void getClose() { Toast.makeText(MainActivity.this, "dododo", Toast.LENGTH_SHORT).show(); } @JavascriptInterface // sdk17版本以上加上注解 public void showSource(String html) { //关键代码 Toast.makeText(MainActivity.this, html, Toast.LENGTH_SHORT).show(); System.out.println("====>html="+html); } }