W3C的优良函数  添加与移除   但是IE8及以下的不支持
addEventListener  和   removeEventListener

window.addEventListener('load',function(){
    var box = document.getElementById('box');
    box.addEventListener('click',function(){
        alert('lee');    //添加多一函数也能够保证不会被覆盖或者只执行一次
    });
    box.addEventListener('click',toBlue,false);
},false);

function toRed(){
    this.className = 'red';
    this.removeEventListener('click',toRed,false);//不会造成卡死问题 也不会是一刀切的问题
    this.addEventListener('click',toBlue,false);
}

function toBlue(){
    this.className = 'blue';
    this.removeEventListener('click',toBlue,false);
    this.addEventListener('click',toRed,false);
}

 

posted on 2018-01-25 21:16  YKing_匆  阅读(119)  评论(0)    收藏  举报