2016-04-25 219 views
0

我试图添加HammerJS:http://hammerjs.github.io/api/HammerJS动态添加元素

动态添加元素。

Hammer.on('.vc', "tap", function (ev) { 
       console.log(ev.type); 
      }); 

但是不起作用。我得到这个错误:

TypeError: target.addEventListener is not a function. (In 'target.addEventListener(type, handler, false)', 'target.addEventListener' is undefined) 

任何帮助,将不胜感激。

回答

1

包装parent-selector.hammer和使用.on处理程序通过hammer-wrapped对象。

$('PARENT_ELEMENT_SELECTOR').hammer().on("tap", ".vc", function() { 
 
    alert("tapped!!"); 
 
});

+0

你测试吗?锤子()功能没有更多的作品。它说未定义。我知道这个代码可以在http://stackoverflow.com/questions/13267364/using-hammer-js-on-dynamically-loaded-content上找到,但是我认为它适用于旧版本。 –

+0

请参阅锤子文档中'jQuery-plugin'提供的示例代码,'$(element).hammer(options).bind(“pan”,myPanHandler);' – Rayon

+0

令人惊叹!谢谢亲爱的!你让我今天一整天都感觉很好。这是我错过的。 –