hammer.js object has no method addEventListener

流过昼夜 提交于 2019-12-19 12:39:38

问题


I am getting the Error: Uncaught TypeError: Object [object Object] has no method 'addEventListener' hammer.js:168

my code is like this:

<script type="text/javascript" src="js/hammer.js"></script>

On device ready function:

 var resim = $('#kaydir');

 Hammer(resim).on('swipeleft', function(ev){
     console.log('left: ', ev);
 });

It seems the error is in hammer.js . What should I do?


回答1:


I imagine your issue is that you don't have Hammer.js's jQuery Plugin installed (GitHub).

Because of this, you cannot pass a jQuery object into the Hammer() function, your two options:

With the jQuery Plugin

Add the jQuery Plugin I've linked to above to your project, then call:

$('#kaydir').Hammer(...)

Without the jQuery Plugin

Pass only the element into Hammer() and not the jQuery object, by using [0]:

Hammer(resim[0]).on(...)

Or instead change your resim variable to hold the result of calling JavaScript's getElementById.

var resim = document.getElementById('kaydir');
Hammer(resim).on(...)



回答2:


If you are using jQuery, you should use the jQuery Hammer version and use it like that:

var resim = $("#kaydir");
resim.hammer().on("swipeleft", function(ev) {
    console.log('left: ', ev);
});


来源:https://stackoverflow.com/questions/28191732/hammer-js-object-has-no-method-addeventlistener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!