使用带有phonegap的touchend事件

时间:2015-10-12 19:08:13

标签: javascript jquery cordova jquery-mobile phonegap-build

我正在使用phonegap开发移动APP,并在点击图片时尝试使用 touchend 事件打开新页面。但它不起作用。

我试过这个,没用:

$('#categories a').on("touchend", function () {
        alert('message');  
    });

而且,这只有在我点击链接时才有效,而不是链接照片:

document.getElementById('touchstart').addEventListener('touchstart', hello, false);
    document.getElementById('touchend').addEventListener('touchend', bye, false);

而且,这不起作用:

var elements =  document.getElementsByClassName('category-item');

    for(var i=0; i<elements.length; i++) { 
        elements[i].addEventListener('touchend', test, false);
    }

      function test () {
          alert('Hey!');
      }

html代码(已成功打印):

$.getJSON( link, {
        type: 'GET',
        format: "json",

    })
    .done(function( data ) {
        all_recipes = '';
        $.each( data, function( i, item ) {
            //console.log(item);
            var element = '';
            element += '<a href="#" category-id="'+ item.id +'" class="category-item col-md-6 col-sm-6 col-xs-12">';
            //element += '<a href="#">';
            element += '<h2>'+ item.name +'</h2>';
            element += '<img src="'+ item.image +'"/>';
            //element += '</a>';
            element += '</a>';

            all_recipes += element;

        });
        $('#categories').html(all_recipes);
    });

知道它是如何工作的吗?我们可以使用JQuery吗?

1 个答案:

答案 0 :(得分:0)

尝试使用touchcancel而不是touch end,这通常可以解决我的问题。

$("#touchstart").on("touchstart", function () {
    alert("touchstart");
});
$("#touchend").on("touchstart", function () {
    alert("touchend");
});
$("#touchcancel").on("touchstart", function () {
    alert("touchcancel");
});

希望这有帮助。

相关问题