Android/Phonegap - onClick() 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10435267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:44:43  来源:igfitidea点击:

Android/ Phonegap - onClick() not working

javascriptandroidhtmlcordova

提问by Raj Labana

I am using phonegap and am trying to detect the onClick, however Android seems to completely ignore it, below is what I am trying:

我正在使用 phonegap 并试图检测 onClick,但是 Android 似乎完全忽略了它,下面是我正在尝试的内容:

<select name="func" onclick="StartButtons();" data-native-menu="true">

I have tried several variations of the onClick include javascript, jQuery etc. however none of these seem to work.

我已经尝试了 onClick 的几种变体,包括 javascript、jQuery 等,但是这些似乎都不起作用。

I have noticed that onChange works, however this is not of much use to me as I need to use a prompt asking the user to confirm the change, if it is no then do nothing. However, using onchange still changes the item within the dropdown.

我注意到 onChange 有效,但是这对我没有多大用处,因为我需要使用提示要求用户确认更改,如果不是,则什么都不做。但是,使用 onchange 仍会更改下拉列表中的项目。

Can someone please help with this?

有人可以帮忙吗?

回答by user1183085

I had the same problem with jQuery, Android seems to ignore the click event in javascript. Originally, my code with jQuery looks like this:

我在 jQuery 上遇到了同样的问题,Android 似乎忽略了 javascript 中的点击事件。最初,我使用 jQuery 的代码如下所示:

$("#element").click(function () { alert("message"); });

I had to change it to:

我不得不将其更改为:

$("#element").on("touchend", function () { alert("message"); });

Not sure if you're using jQuery at all, but hope this helps.

不确定您是否正在使用 jQuery,但希望这会有所帮助。

回答by Chemik

Add click event listener to your object with javascript. First add an id to object:

使用 javascript 将单击事件侦听器添加到您的对象。首先给对象添加一个id:

    <select id="obj_id" name="func" data-native-menu="true">

And then call addEventListener() :

然后调用 addEventListener() :

    document.getElementById("obj_id").addEventListener("click", StartButtons, false);

Phonegap documentation recomends to run all your javascript code in "deviceready" event. So your code should look like this:

Phonegap 文档建议在“deviceready”事件中运行所有 javascript 代码。所以你的代码应该是这样的:

main.js:

主要.js:

    function StartButtons() { ... }

    function onDeviceReady() {
        ...
        document.getElementById("obj_id")
            .addEventListener("click", StartButtons, false);
        ...
    }

    function init() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }

index.html:

索引.html:

    ...
        <script type="text/javascript" charset="utf-8" src="main.js"></script>
    </head>
    <body onload="init();">
        ...
        <select id="obj_id" name="func" data-native-menu="true">
        ...
    </body>

回答by aakash tandukar

https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">

https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; 媒体源 *">

delete this code from index.html

从 index.html 中删除此代码

回答by Prometeo

user1183085′s answer is the right one. Android does not recognize click events as web apps do, this makes sence since mobile apps are not web when native, so that is why phonegap was made but since it works with jquery and not native java android libraries directly. I put my code this way:

user1183085 的回答是正确的。Android 无法像 Web 应用程序那样识别点击事件,这是有道理的,因为移动应用程序在原生时不是 Web,所以这就是制作 phonegap 的原因,但因为它直接与 jquery 而不是原生 java android 库一起使用。我把我的代码这样写:

The web buttons or elements onclick to handle all web interfaces, and special buttons or elements ontouchend to handle the mobile interfaces. Here′s a part of my code:

web 按钮或元素 onclick 处理所有 web 界面,特殊按钮或元素 ontouchend 处理移动界面。这是我的代码的一部分:

//this handles the login button events for the mobile touch of the user
$("button.login-button").on("touchend", function () {alert('hello touch ! =o' )});

//this handles the login button events for web clicks
$("button.login-button").on("click", function () {alert('hello click ! =o' )});

thanks user user1183085, that solved my life forever =()

感谢用户 user1183085,它永远解决了我的生活 =()

回答by JBENOIT

Watch out, bind an onclick on an input in Cordova 3.4, doesn't work. But it works great with a div element. Considering, input elements are part of form element, like select element, it may be your problem.

注意,在 Cordova 3.4 中绑定一个输入的 onclick 是行不通的。但它适用于 div 元素。考虑到输入元素是表单元素的一部分,就像选择元素一样,这可能是你的问题。

回答by thomas.cloud

Have you tried setting your method to be called to "method" instead of "method();"? So maybe you could try your line as:

您是否尝试将您的方法设置为“method”而不是“method();”?所以也许你可以试试你的线路:

<select name="func" onclick="StartButtons" data-native-menu="true">

Then hopefully your method will be called.

然后希望你的方法会被调用。