jQuery函数不适用于Hammer

jQuery函数不适用于Hammer

本文介绍了jQuery函数不适用于Hammer.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将press添加到jQuery选择器.我在同一文档中有很多元素,所以我不能为每个元素使用ID.我由$(selector)[i]尝试过,就像解释的 .

I am trying to add press to jQuery selector. I have many elements on same document, So I can not use IDs for each. I tried by $(selector)[i] as like explained here.

var selectProduct = $('.mh60 a');
for (var i = 0; i < selectProduct.length; i++) {
  Hammer(selectProduct[i]).on("press", function() {
     $(selectProduct[i]).addClass('active');
  });
}

它没有产生任何错误并且无法正常工作.我没有得到我在这里想念的东西.

It's not producing any error and not working. I didn't get what I am missing here.

当我尝试通过console.log(selectProduct[i]);登录selectProduct[i]时,它会给出undefined结果.

And when I try to log selectProduct[i] by console.log(selectProduct[i]); it gives undefined result.

更新1

当我删除for循环并仅使用selectProduct[0]selectProduct[1]时,...可以正常工作,但是使用selectProduct[i]时,它却无法正常工作,所以我认为问题出在for循环上.但是我不明白.

When I remove for loop and just use selectProduct[0] , selectProduct[1] , ... it's working but with selectProduct[i] , it's not working, So I think problem is on for loop. But I didn't get it.

更新2

我还尝试了 jQuery插件,同样的问题

I also tried with jQuery plugin, same problem

更新3

我再次尝试使用each(),同样的问题.它显示控制台消息,但addClass()不起作用.我想问题出在this函数,它不返回当前元素.

Again I tried with each(), same problem. It print the console message but addClass() is not working. I guess the problem is with this function which is not returning the current element.

$('.mh60 a').each(function(){
    var mc = new Hammer(this);
    mc.on("press", function() {
      console.log('Double tap!');
      $(this).addClass('active');
    });
 });

推荐答案

最后,我通过使用each函数

$('.mh60 a').each(function(){
    var mc = new Hammer(this);
    var currentEle = $(this);
    mc.on("press", function() {
      currentEle.addClass('active');
    });
 });

这篇关于jQuery函数不适用于Hammer.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 20:16