在某些情况下,javascript .click()方法无法在android上运行

时间:2014-03-24 12:23:30

标签: javascript android eclipse web webview

链接的HTML代码:

<a href="#" id="10" target="_blank">
    some codes here...
</a>

我打电话给我申请的代码:

webView.loadUrl("javascript:(function(){document.getElementById('10').click();})()");

但没有结果......

我之前为我的webView启用了javascript并且它可以运行... 此外,我还测试了其他网页上其他元素的.click()方法,并且工作正常。

如何使webView点击链接...?请帮帮我;谢谢!


EDIT :

Perhaps it's because of the numeric start of id name of these elements,
    but I can use them using their id or tag name on
    google chrome console on windows without any problem...

but It's not possible to use them using their tag name, even, to click on, on android webView !

EDIT 2 :

The problem isn't numeric start of 'id's in fact !
I've found out that click function doesn't work even for other ids without numerics
    IN THIS PAGE I'M USING ONLY...
        AND THIS IS WHAT I CAN'T UNDERSTAND WHY !!!
I've tried creating fireEvent function, too... FAIL AGAIN !


I SHOULD ADD THAT THE HTML CODE OF THE PAGE IS SOMETHING LIKE THIS IN FACT :

    <div class="abc"><a href="#" id="defg"></a></div>

EDIT 3 : Sorry I forgot to say that these elements should open a window in the page
    when they are called by click.

I found out that when I click them using javascript on android,
    the html source of the page changes ( so the Click is being called as we need )
    , but the window doesn't open and it's data are not received...

EDIT 4 : I have used these codes after defining my webView, so what's the problem exactly ???

    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    webView.setWebViewClient(client);
    webView.setWebChromeClient(new WebChromeClient());
    webView.addJavascriptInterface(iface, "droid");

1 个答案:

答案 0 :(得分:0)

我认为ID和NAME必须以字母开头。尝试将id =“10”更改为id =“i-10”,并在javascript调用中进行更改。