Android - Javascript: how to execute jquery in webview

六眼飞鱼酱① 提交于 2020-01-03 18:35:11

问题


I'm building an app which load a webpage in a webview. In that webpage, i need to programmatically click on some links using Jquery. Now, i know how to execute a Javascript code on the webview programmatically (see below):

WebSettings myBrowserSettings = myBrowser.getSettings();
myBrowserSettings.setJavaScriptEnabled(true);
Log.d("Stefano", "JS enabled");    


myBrowser.loadUrl("javascript:document.getElementsByid('myWord').click();");

But now, I need to know how implement a Jquery function in my webview; i'm looking for the correct way to manage something like:

myBrowser.loadUrl("jquery:function($("#myAnchor").click(function(event){})");

And which is the correct way to implement the following function?

$("#a_link")[0].click();

回答1:


If jquery loaded in this page, you can just call this:

webview.setWebViewClient(new WebViewClient() {  
    @Override  
    public void onPageFinished(WebView view, String url) {  
        webview.loadUrl("javascript:(function() { " +  
                    "$("#myAnchor").click(function(event){}" +  
                    "})()");  
    }  
});



回答2:


What's your problem? It's about escaping characters?

myBrowser.loadUrl("jquery:function($(\"#myAnchor\").click(function(event){})");


来源:https://stackoverflow.com/questions/28578508/android-javascript-how-to-execute-jquery-in-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!