1

我正在使用 .on() 将触摸事件与元素绑定,但我收到错误 e 未定义。怎么办?我尝试在括号内写事件 e,但它不起作用。

$("#myImageFlow ").on('click touchstart',".sliderImage",abc);
//custom function handler for event                  
function abc(){

 e.stopPropagation(); e.preventDefault();

console.log('has no method ...................'+e);

如何定义事件变量,因为我没有用()调用函数?

4

4 回答 4

4

abc是事件处理程序,所以你需要使用function abc(e){}

$("#myImageFlow ").on('click touchstart', ".sliderImage", abc);
//custom function handler for event                  
function abc(e) {
    e.stopPropagation();
    e.preventDefault();
    console.log('has no method ...................' + e);
}
于 2013-06-17T11:20:54.583 回答
2

定义e为参数:

function abc(e){
    if(typeof(e) != "undefined" && e.type == "click"){ // only call stopPropagation and preventDefault on click events
        e.stopPropagation(); e.preventDefault();
    }

    console.log('has no method ...................'+e);
}

或者,您可以有选择地传递参数:

$("#myImageFlow ")
    .on('click',".sliderImage", function(e){
        abc(e);
    })
    .on('touchstart',".sliderImage", function(){
        abc();
    });
于 2013-06-17T11:20:44.603 回答
1

您的参考函数将事件作为参数:-

function abc(e){

 e.stopPropagation(); e.preventDefault();

console.log('has no method ...................'+e);
}
于 2013-06-17T11:20:43.763 回答
0

为避免混淆

$("#myImageFlow ").on('click touchstart',".sliderImage", function(event){
  alert(event);
});
于 2013-06-17T11:21:02.923 回答