Android - Javascript:如何在webview中执行jquery

时间:2020-11-28 20:44:23

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):

我正在构建一个在webview中加载网页的应用程序。在该网页中,我需要使用Jquery以编程方式单击某些链接。现在,我知道如何以编程方式在webview上执行Javascript代码(见下文):

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:

但现在,我需要知道如何在我的webview中实现Jquery函数;我正在寻找管理以下内容的正确方法:

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

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

哪个是实现以下功能的正确方法?

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

2 个解决方案

#1


3  

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

你怎么了?这是关于逃避角色?

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

#2


3  

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

如果在此页面中加载了jquery,则可以调用此方法:

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

#1


3  

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

你怎么了?这是关于逃避角色?

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

#2


3  

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

如果在此页面中加载了jquery,则可以调用此方法:

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