我有这个自我执行的功能,它将事件侦听器添加到一系列链接。但是,与页面onmouseover / out相反,事件侦听器会在页面加载时自动触发。我的语法有误吗?如何停止自动点火?

;(function(){
    var menuLink = $('.navItem');
    var menuItem = $('.menuLinks');
    for (a = 0; a <= 5; ++a) {
        menuLink[a].addEventListener('onmouseover', linkChanger(), true);
        menuLink[a].addEventListener('onmouseout', linkChanger(), true);
    }

    function linkChanger(event){
        if (menuItem[a].style.color == "white") {
            console.log("This is white")
            menuItem[a].style.color = "black";
        }
        else {
            console.log("This is black");
            menuItem[a].style.color = "white";
        }
    }
})()

最佳答案

menuLink[a].addEventListener('onmouseover', linkChanger, true);
menuLink[a].addEventListener('onmouseout', linkChanger, true);

07-24 20:22