gpt4 book ai didi

php - 使用javascript更改背景颜色

转载 作者:太空宇宙 更新时间:2023-11-04 15:35:33 25 4
gpt4 key购买 nike

此脚本在除 IE 之外的所有其他浏览器中都可以正常工作:

nav.addEventListener('mouseover',function(e) {
switch(e.target.id) {
case 'aGallery':
navOpacity.style.backgroundColor = "red";
break;
case 'aContact':
navOpacity.style.backgroundColor = "green";
break;
case 'aAbout':
navOpacity.style.backgroundColor = "yellow";
break;
case 'aHome':
navOpacity.style.backgroundColor = "#CC33CC";
break;
}
},false);

在 IE 中,悬停时背景颜色不会改变。

有什么想法吗?

最佳答案

在 IE 中,您必须使用 attachEvent 而不是标准的 addEventListener。并为 IE 使用 srcElement 而不是 target

试试这个。

function mouseOverHandler(e) {
switch((e.target || e.srcElement).id) {
case 'aGallery':
navOpacity.style.backgroundColor = "red";
break;
case 'aContact':
navOpacity.style.backgroundColor = "green";
break;
case 'aAbout':
navOpacity.style.backgroundColor = "yellow";
break;
case 'aHome':
navOpacity.style.backgroundColor = "#CC33CC";
break;
}
}

if (el.addEventListener){
el.addEventListener('mouseover', mouseOverHandler, false);
}
else if (el.attachEvent){
el.attachEvent('onmouseover', mouseOverHandler);
}

关于php - 使用javascript更改背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9204333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com