想知道是否有人可以提供一些见解,为什么这个js在Firefox中不起作用但是在IE和Chrome下完全正常工作?
当鼠标悬停在页面顶部的某些按钮上时,它会改变很多div的颜色。
可在我的网站上看到:ptable.info
window.onload=function(){
var noblegasesColl = document.getElementsByClassName('noblegases');
var halogensColl = document.getElementsByClassName('halogens');
var othernonmetalsColl = document.getElementsByClassName('othernonmetals');
var transitionmetalsColl = document.getElementsByClassName('transitionmetals');
var metalloidsColl = document.getElementsByClassName('metalloids');
var posttransitionColl = document.getElementsByClassName('posttransition');
var alkalineColl = document.getElementsByClassName('alkaline');
var alkaliColl = document.getElementsByClassName('alkali');
var actinoidsColl = document.getElementsByClassName('actinoids');
var lanthanoidsColl = document.getElementsByClassName('lanthanoids');
//Noble Gases
document.getElementById('noblegasesButton').addEventListener('mouseover', function(){
changeColor(halogensColl, '#696969');
changeColor(othernonmetalsColl, '#696969');
changeColor(transitionmetalsColl, '#696969');
changeColor(metalloidsColl, '#696969');
changeColor(posttransitionColl, '#696969');
changeColor(alkalineColl, '#696969');
changeColor(alkaliColl, '#696969');
changeColor(actinoidsColl, '#696969');
changeColor(lanthanoidsColl, '#696969');
});
document.getElementById('noblegasesButton').addEventListener('mouseout', function(){
changeColor(halogensColl, '#00FFFF');
changeColor(othernonmetalsColl, '#7CFC00');
changeColor(transitionmetalsColl, '#ff78bb');
changeColor(metalloidsColl, '#7A378B');
changeColor(posttransitionColl, '#8FBC8F');
changeColor(alkalineColl, '#e5e500');
changeColor(alkaliColl, '#FF6600');
changeColor(actinoidsColl, '#f4a460');
changeColor(lanthanoidsColl, '#CC1100');
changeColor(actinoidsColl, '#f4a460');
});
//Halogens
document.getElementById('halogensButton').addEventListener('mouseover', function(){
changeColor(noblegasesColl, '#696969');
changeColor(othernonmetalsColl, '#696969');
changeColor(transitionmetalsColl, '#696969');
changeColor(metalloidsColl, '#696969');
changeColor(posttransitionColl, '#696969');
changeColor(alkalineColl, '#696969');
changeColor(alkaliColl, '#696969');
changeColor(actinoidsColl, '#696969');
changeColor(lanthanoidsColl, '#696969');
});
document.getElementById('halogensButton').addEventListener('mouseout', function(){
changeColor(halogensColl, '#00FFFF');
changeColor(othernonmetalsColl, '#7CFC00');
changeColor(transitionmetalsColl, '#ff78bb');
changeColor(metalloidsColl, '#7A378B');
changeColor(posttransitionColl, '#8FBC8F');
changeColor(alkalineColl, '#e5e500');
changeColor(alkaliColl, '#FF6600');
changeColor(noblegasesColl, '#3399CC');
changeColor(lanthanoidsColl, '#CC1100');
changeColor(actinoidsColl, '#f4a460');
});
//Non-metals
document.getElementById('nonmetalsButton').addEventListener('mouseover', function(){
changeColor(halogensColl, '#696969');
changeColor(noblegasesColl, '#696969');
changeColor(transitionmetalsColl, '#696969');
changeColor(metalloidsColl, '#696969');
changeColor(posttransitionColl, '#696969');
changeColor(alkalineColl, '#696969');
changeColor(alkaliColl, '#696969');
changeColor(actinoidsColl, '#696969');
changeColor(lanthanoidsColl, '#696969');
});
document.getElementById('nonmetalsButton').addEventListener('mouseout', function(){
changeColor(halogensColl, '#00FFFF');
changeColor(othernonmetalsColl, '#7CFC00');
changeColor(transitionmetalsColl, '#ff78bb');
changeColor(metalloidsColl, '#7A378B');
changeColor(posttransitionColl, '#8FBC8F');
changeColor(alkalineColl, '#e5e500');
changeColor(alkaliColl, '#FF6600');
changeColor(noblegasesColl, '#3399CC');
changeColor(lanthanoidsColl, '#CC1100');
changeColor(actinoidsColl, '#f4a460');
});
//Transition metals
document.getElementById('transitionmetalsButton').addEventListener('mouseover', function(){
changeColor(halogensColl, '#696969');
changeColor(othernonmetalsColl, '#696969');
changeColor(noblegasesColl, '#696969');
changeColor(metalloidsColl, '#696969');
changeColor(posttransitionColl, '#696969');
changeColor(alkalineColl, '#696969');
changeColor(alkaliColl, '#696969');
changeColor(actinoidsColl, '#696969');
changeColor(lanthanoidsColl, '#696969');
});
document.getElementById('transitionmetalsButton').addEventListener('mouseout', function(){
changeColor(halogensColl, '#00FFFF');
changeColor(othernonmetalsColl, '#7CFC00');
changeColor(transitionmetalsColl, '#ff78bb');
changeColor(metalloidsColl, '#7A378B');
changeColor(posttransitionColl, '#8FBC8F');
changeColor(alkalineColl, '#e5e500');
changeColor(alkaliColl, '#FF6600');
changeColor(noblegasesColl, '#3399CC');
changeColor(lanthanoidsColl, '#CC1100');
changeColor(actinoidsColl, '#f4a460');
});
//Post-transition
document.getElementById('posttransitionButton').addEventListener('mouseover', function(){
changeColor(halogensColl, '#696969');
changeColor(othernonmetalsColl, '#696969');
changeColor(transitionmetalsColl, '#696969');
changeColor(metalloidsColl, '#696969');
changeColor(noblegasesColl, '#696969');
changeColor(alkalineColl, '#696969');
changeColor(alkaliColl, '#696969');
changeColor(actinoidsColl, '#696969');
changeColor(lanthanoidsColl, '#696969');
});
document.getElementById('posttransitionButton').addEventListener('mouseout', function(){
changeColor(halogensColl, '#00FFFF');
changeColor(othernonmetalsColl, '#7CFC00');
changeColor(transitionmetalsColl, '#ff78bb');
changeColor(metalloidsColl, '#7A378B');
changeColor(posttransitionColl, '#8FBC8F');
changeColor(alkalineColl, '#e5e500');
changeColor(alkaliColl, '#FF6600');
changeColor(noblegasesColl, '#3399CC');
changeColor(lanthanoidsColl, '#CC1100');
changeColor(actinoidsColl, '#f4a460');
});
//Metalloids
document.getElementById('metalloidsButton').addEventListener('mouseover', function(){
changeColor(halogensColl, '#696969');
changeColor(othernonmetalsColl, '#696969');
changeColor(transitionmetalsColl, '#696969');
changeColor(noblegasesColl, '#696969');
changeColor(posttransitionColl, '#696969');
changeColor(alkalineColl, '#696969');
changeColor(alkaliColl, '#696969');
changeColor(actinoidsColl, '#696969');
changeColor(lanthanoidsColl, '#696969');
});
document.getElementById('metalloidsButton').addEventListener('mouseout', function(){
changeColor(halogensColl, '#00FFFF');
changeColor(othernonmetalsColl, '#7CFC00');
changeColor(transitionmetalsColl, '#ff78bb');
changeColor(metalloidsColl, '#7A378B');
changeColor(posttransitionColl, '#8FBC8F');
changeColor(alkalineColl, '#e5e500');
changeColor(alkaliColl, '#FF6600');
changeColor(noblegasesColl, '#3399CC');
changeColor(lanthanoidsColl, '#CC1100');
changeColor(actinoidsColl, '#f4a460');
});
//Alkaline
document.getElementById('alkalineButton').addEventListener('mouseover', function(){
changeColor(halogensColl, '#696969');
changeColor(othernonmetalsColl, '#696969');
changeColor(transitionmetalsColl, '#696969');
changeColor(metalloidsColl, '#696969');
changeColor(posttransitionColl, '#696969');
changeColor(noblegasesColl, '#696969');
changeColor(alkaliColl, '#696969');
changeColor(actinoidsColl, '#696969');
changeColor(lanthanoidsColl, '#696969');
});
document.getElementById('alkalineButton').addEventListener('mouseout', function(){
changeColor(halogensColl, '#00FFFF');
changeColor(othernonmetalsColl, '#7CFC00');
changeColor(transitionmetalsColl, '#ff78bb');
changeColor(metalloidsColl, '#7A378B');
changeColor(posttransitionColl, '#8FBC8F');
changeColor(alkalineColl, '#e5e500');
changeColor(alkaliColl, '#FF6600');
changeColor(noblegasesColl, '#3399CC');
changeColor(lanthanoidsColl, '#CC1100');
changeColor(actinoidsColl, '#f4a460');
});
//Alkali
document.getElementById('alkaliButton').addEventListener('mouseover', function(){
changeColor(halogensColl, '#696969');
changeColor(othernonmetalsColl, '#696969');
changeColor(transitionmetalsColl, '#696969');
changeColor(metalloidsColl, '#696969');
changeColor(posttransitionColl, '#696969');
changeColor(alkalineColl, '#696969');
changeColor(noblegasesColl, '#696969');
changeColor(actinoidsColl, '#696969');
changeColor(lanthanoidsColl, '#696969');
});
document.getElementById('alkaliButton').addEventListener('mouseout', function(){
changeColor(halogensColl, '#00FFFF');
changeColor(othernonmetalsColl, '#7CFC00');
changeColor(transitionmetalsColl, '#ff78bb');
changeColor(metalloidsColl, '#7A378B');
changeColor(posttransitionColl, '#8FBC8F');
changeColor(alkalineColl, '#e5e500');
changeColor(alkaliColl, '#FF6600');
changeColor(noblegasesColl, '#3399CC');
changeColor(lanthanoidsColl, '#CC1100');
changeColor(actinoidsColl, '#f4a460');
});
//Lanthanoids
document.getElementById('lanthanoidsButton').addEventListener('mouseover', function(){
changeColor(halogensColl, '#696969');
changeColor(othernonmetalsColl, '#696969');
changeColor(transitionmetalsColl, '#696969');
changeColor(metalloidsColl, '#696969');
changeColor(posttransitionColl, '#696969');
changeColor(alkalineColl, '#696969');
changeColor(alkaliColl, '#696969');
changeColor(actinoidsColl, '#696969');
changeColor(noblegasesColl, '#696969');
});
document.getElementById('lanthanoidsButton').addEventListener('mouseout', function(){
changeColor(halogensColl, '#00FFFF');
changeColor(othernonmetalsColl, '#7CFC00');
changeColor(transitionmetalsColl, '#ff78bb');
changeColor(metalloidsColl, '#7A378B');
changeColor(posttransitionColl, '#8FBC8F');
changeColor(alkalineColl, '#e5e500');
changeColor(alkaliColl, '#FF6600');
changeColor(noblegasesColl, '#3399CC');
changeColor(lanthanoidsColl, '#CC1100');
changeColor(actinoidsColl, '#f4a460');
});
//Actinoids
document.getElementById('actinoidsButton').addEventListener('mouseover', function(){
changeColor(halogensColl, '#696969');
changeColor(othernonmetalsColl, '#696969');
changeColor(transitionmetalsColl, '#696969');
changeColor(metalloidsColl, '#696969');
changeColor(posttransitionColl, '#696969');
changeColor(alkalineColl, '#696969');
changeColor(alkaliColl, '#696969');
changeColor(noblegasesColl, '#696969');
changeColor(lanthanoidsColl, '#696969');
});
document.getElementById('actinoidsButton').addEventListener('mouseout', function(){
changeColor(halogensColl, '#00FFFF');
changeColor(othernonmetalsColl, '#7CFC00');
changeColor(transitionmetalsColl, '#ff78bb');
changeColor(metalloidsColl, '#7A378B');
changeColor(posttransitionColl, '#8FBC8F');
changeColor(alkalineColl, '#e5e500');
changeColor(alkaliColl, '#FF6600');
changeColor(noblegasesColl, '#3399CC');
changeColor(lanthanoidsColl, '#CC1100');
changeColor(actinoidsColl, '#f4a460');
});
}
function changeColor(coll, color){
for(var i=0, len=coll.length; i<len; i++)
{
coll[i].style["background-color"] = color;
}
}
答案 0 :(得分:1)
您必须使用style.backgroundColor
或style['backgroundColor']
来设置背景颜色。这些方法中的任何一种都应该适用于浏览器。我已经在FireFox 23,Opera 15,Chrome 31(Dev Build)和IE10中进行了测试。
您可以参考here获取样本小提琴。小提琴有三个div
,id
分别为aa
,bb
和cc
。
当您执行mouseover
和mouseout
时,您会发现它会影响Chrome,IE10和Opera中的所有三个div
。但在FireFox中,它仅影响第一个和第三个div
,因为第二个div
使用style['background-color']
而FireFox无法识别。