javascript hammer.js 对象没有方法 addEventListener
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28191732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
hammer.js object has no method addEventListener
提问by hdayi
I am getting the Error: Uncaught TypeError: Object [object Object] has no method 'addEventListener' hammer.js:168
我收到错误: 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?
错误似乎在hammer.js 中。我该怎么办?
回答by James Donnelly
I imagine your issue is that you don't have Hammer.js's jQuery Plugininstalled (GitHub).
我想你的问题是你没有安装Hammer.js 的 jQuery 插件(GitHub)。
Because of this, you cannot pass a jQuery object into the Hammer()
function, your two options:
因此,您不能将 jQuery 对象传递给Hammer()
函数,您有两个选择:
With the jQuery Plugin
使用 jQuery 插件
Add the jQuery Plugin I've linked to above to your project, then call:
将我在上面链接到的 jQuery 插件添加到您的项目中,然后调用:
$('#kaydir').Hammer(...)
Without the jQuery Plugin
没有 jQuery 插件
Pass only the element into Hammer()
and not the jQuery object, by using [0]
:
只将元素传递到Hammer()
jQuery 对象,而不是传递给 jQuery 对象,使用[0]
:
Hammer(resim[0]).on(...)
Or instead change your resim
variable to hold the result of calling JavaScript's getElementById
.
或者改为更改您的resim
变量以保存调用 JavaScript 的getElementById
.
var resim = document.getElementById('kaydir');
Hammer(resim).on(...)
回答by flks
If you are using jQuery, you should use the jQuery Hammer versionand use it like that:
如果你使用 jQuery,你应该使用jQuery Hammer 版本并像这样使用它:
var resim = $("#kaydir");
resim.hammer().on("swipeleft", function(ev) {
console.log('left: ', ev);
});