[英]changing backgroundColor using javascript
This script works fine in all other browsers except IE: 该脚本在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);
In IE, the backgroundcolor does not change on hover. 在IE中,backgroundcolor不会在悬停时更改。
Any ideas? 有任何想法吗?
In IE you have to use attachEvent
rather than the standard addEventListener
. 在IE中,您必须使用
attachEvent
而不是标准的addEventListener
。 And use srcElement
instead of target
for IE. 并使用
srcElement
代替IE的target
。
Try this. 尝试这个。
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);
}
IE uses the 'on' versions for events, line onclick
, onmouseover
. IE使用on事件版本,
onclick
行, onmouseover
。 There should be your problem. 应该是你的问题。
Also, IE (prior to version 9), does not support addEventListener
. 另外,IE(版本9之前的版本)不支持
addEventListener
。 You must use attachEvent
. 您必须使用
attachEvent
。
Consider this code as a starting point: 将此代码视为起点:
if (el.addEventListener){
el.addEventListener('click', myFunc);
} else if (el.attachEvent){
el.attachEvent('onclick', myFunc);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.