是否可以在DIV上添加事件监听器? [英] Is it possible to add an eventlistener on a DIV?
本文介绍了是否可以在DIV上添加事件监听器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道这个功能:
document.addEventListener('touchstart', function(event) {
alert(event.touches.length);
}, false);
但是可以将它添加到div示例:
But is it possible to add this to a div? Example:
document.getElementById("div").addEventListener('touchstart', function(event) {
alert(event.touches.length);
}, false);
我还没有测试过,也许有些人知道吗?
I haven't tested it yet, maybe some of you guys knows it?
推荐答案
是的,这是你怎么做的。
Yeah, that's how you do it.
document.getElementById("div").addEventListener("touchstart", touchHandler, false);
document.getElementById("div").addEventListener("touchmove", touchHandler, false);
document.getElementById("div").addEventListener("touchend", touchHandler, false);
function touchHandler(e) {
if (e.type == "touchstart") {
alert("You touched the screen!");
} else if (e.type == "touchmove") {
alert("You moved your finger!");
} else if (e.type == "touchend" || e.type == "touchcancel") {
alert("You removed your finger from the screen!");
}
}
或使用jQuery
$(function(){
$("#div").bind("touchstart", function (event) {
alert(event.touches.length);
});
});
这篇关于是否可以在DIV上添加事件监听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文