模拟app 长按后触发事件
$.fn.longPress = function(fn) {
var timeout = undefined;
var $this = this;
for(var i = 0;i<$this.length;i++){
$this[i].addEventListener('touchstart', function(event) {
timeout = setTimeout(fn, 800);
}, false);
$this[i].addEventListener('touchend', function(event) {
clearTimeout(timeout);
}, false);
}
};
//cmt01动态添加的元素
$('.cmt01').each(function(e){
//var _this = $(this);
$(this).longPress(function(){
alert(1)
});
})
最简单的解释就是:
解决办法:事件委托。