【问题标题】:javascript function is not working in cordovajavascript函数在科尔多瓦不起作用
【发布时间】:2017-09-13 11:26:27
【问题描述】:

在科尔多瓦我想从监听器调用一个按钮事件。在我的应用程序中,我正在加载一个页面 http://example.org/app 。在那个页面有一个按钮,它的类名是 btn。我想在单击该按钮时显示警报。网页的html代码如下所示。 <p><a class="btn">Click</a></p>

index.js 中的cordova 代码

var app = {
    // Application Constructor
    initialize: function () {
        this.bindEvents();
    },
    bindEvents: function () {
        document.addEventListener('deviceready', this.onDeviceReady, false);        
    },
    onDeviceReady: function () {
    app.receivedEvent('deviceready');
    // Here, we redirect to the web site.
    var targetUrl = "http://example.org/app";
    var bkpLink = document.getElementById("bkpLink");
    bkpLink.setAttribute("href", targetUrl);
    bkpLink.text = targetUrl;
    window.location.replace(targetUrl);
    //I want to call a alert when button click      
    document.getElementsByClassName('btn')[0].addEventListener('click', (function(i) {
    return function() {
        alert("okkkkkkkkkk");
    };
    })(i), false);  

    },
    // Note: This code is taken from the Cordova CLI template.
    receivedEvent: function (id) {
    }

};
app.initialize();

当我使用此代码时,它不起作用。 如何监听加载网页的点击事件。请帮帮我。

【问题讨论】:

  • 你定义了i在点击事件中调用函数的变量吗?
  • 不,我没有定义 i 变量。
  • 如果你想在 jquery 中做一个简单的函数,$('.btn').click(function(){ alert('okkkk')});
  • 它在科尔多瓦工作吗?
  • 尝试定义或删除,也可以直接制作addEventListener('click', function() { alert('ok'); })

标签: javascript jquery asp.net cordova phonegap


【解决方案1】:

请你试试这个。我对将在点击时执行的功能进行了更改。

var app = {
    // Application Constructor
    initialize: function () {
        this.bindEvents();
    },
    bindEvents: function () {
        document.addEventListener('deviceready', this.onDeviceReady, false);        
    },
    onDeviceReady: function () {
    app.receivedEvent('deviceready');
    // Here, we redirect to the web site.
    var targetUrl = "http://example.org/app";
    var bkpLink = document.getElementById("bkpLink");
    bkpLink.setAttribute("href", targetUrl);
    bkpLink.text = targetUrl;
    window.location.replace(targetUrl);
    //I want to call a alert when button click      
    document.getElementsByClassName('btn')[0].addEventListener('click', function(i) {
        alert("okkkkkkkkkk");
    }, false);  

    },
    // Note: This code is taken from the Cordova CLI template.
    receivedEvent: function (id) {
    }

};
app.initialize();

【讨论】:

    【解决方案2】:

    您正在应用程序中加载外部网站。所以你必须在外部站点而不是在你的应用程序内部编写点击函数。

    window.location.replace(targetUrl); 后面的代码不起作用。

    【讨论】:

    • 我正在加载外部站点,但我们可以调用 click 函数。我从 docs.microsoft.com/en-us/visualstudio/cross-platform/… 得到了代码。请参阅向 Cordova 应用程序添加相机功能
    • window.location.replace(targetUrl);后面的代码不管用。为了使它工作,你需要在你的外部站点中编写点击函数。
    • 您是否检查过上面的链接。我认为这是可能的。在该参考站点中,他们正在调用外部网页。
    • 我已经删除了 window.location.replace(targetUrl); .之后也无法正常工作。
    猜你喜欢
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 2016-08-18
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    相关资源
    最近更新 更多