javascript を使用して eventListener を追加するのに問題があります。わかりました、4 つのアンカー要素を作成する関数があります。背景色を変更する関数を呼び出す onmouseover にイベントを追加したいと考えています。コードは次のとおりです (createAnchor() の最後の行から次の行を見て、関連するコード行を見つけてください。
function createAanchor(index) {
var a = document.createElement("a");
var text = getText(index);
var a = document.createElement("a");
var t = document.createTextNode(text);
a.href = getHref(index);
a.appendChild(t);
a.style.textAlign = "center";
a.style.fontSize = "1.2em";
a.style.color = "white";
a.style.fontFamily = "arial";
a.style.fontWeight = "bold";
a.style.textDecoration = "none";
a.style.lineHeight = "238px";
a.style.width = "238px";
a.style.margin = "5px";
a.style.background = eightColors(index);
a.style.position = "absolute";
a.addEventListener("onmouseover", changeColor());
return a;
}
function changeColor() {
alert("EVENT WORKING");
}
ここに問題があります。関数が実行されa.addEventListener("onmouseover", changeColor());
たfunction changeColors()
が、後で実行されない場合、これはonmouseover
なぜですか?