1. 程式人生 > >JavaScript中如何使addEventListener繫結的函式既能傳參還能解綁?

JavaScript中如何使addEventListener繫結的函式既能傳參還能解綁?

    var btn = document.getElementsByClassName('btn')[0],
        remove = document.getElementsByClassName('remove')[0];
    var some = function (msg) {
        console.log(msg)
    };
    // bind 方法pc端有相容性問題,請注意
    var fn = some.bind(null,'哈哈');
    btn.addEventListener('click',fn,false);
    remove.addEventListener
('click',function () { btn.removeEventListener('click',fn,false); },false);