手機端js模擬長按事件(代碼仿照jQuery)

代碼編寫:javascript

$.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);
	}
}

代碼使用:java

$(select).longPress(function(){
	alert(1);
});
相關文章
相關標籤/搜索