如何同时传event以及其他参数

有时方法需要同时传递event防止冒泡和其他参数,因此可以将方法进行封装。

function x(e,str){
    var a=e||window.event;
    if(a.stopPropagation){
        a.stopPropagation();
    }else{
        a.cancelBubble=true;
    }
    alert(str);
}

window.onload=function(){
    document.getElementById('span').onclick=function(e){
        x(e,'mm');
    }
}

或者一种更简单的

function x(e){
    var a=e||window.event;
    if(a.stopPropagation){
        a.stopPropagation();
    }else{
        a.cancelBubble=true;
    }
}

window.onload=function(){
    document.getElementById('span').onclick=function(e,str){
        x(e);
        alert(str);
    }
}

欢迎访问个人博客:cheeseyu.cn

你可能感兴趣的:(Javascript)