问题
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