火狐下的一段JS报错ReferenceError: event is not defined ,请问怎么解决。
我的报错的JS 是一段表格隔行换色的JS ,如下所示:
<script>
var highlightcolor = '#c1ebff';
var clickcolor = '#51b2f6';
function changeto() {
source = event.srcElement;//此处报错
if (source.tagName == "TR" || source.tagName == "TABLE")
return;
while (source.tagName != "TD")
source = source.parentElement;
source = source.parentElement;
cs = source.children;
//alert(cs.length);
if (cs[1].style.backgroundColor != highlightcolor && source.id != "nc"
&& cs[1].style.backgroundColor != clickcolor)
for (i = 0; i < cs.length; i++) {
cs[i].style.backgroundColor = highlightcolor;
}
}
function changeback() {
if (event.fromElement.contains(event.toElement)//此处报错
|| source.contains(event.toElement) || source.id == "nc")
return
if (event.toElement!=source&&cs[1].style.backgroundColor!=clickcolor)
//source.style.backgroundColor=originalcolor
for(i=0;i<cs.length;i++){
cs[i].style.backgroundColor="";
}
}
function clickto(){
source=event.srcElement;
if (source.tagName=="TR"||source.tagName=="TABLE")
return;
while(source.tagName!="TD")
source=source.parentElement;
source=source.parentElement;
cs = source.children;
//alert(cs.length);
if (cs[1].style.backgroundColor!=clickcolor&&source.id!="nc")
for(i=0;i<cs.length;i++){
cs[i].style.backgroundColor=clickcolor;
}
else
for(i=0;i<cs.length;i++){
cs[i].style.backgroundColor="";
}
}
</script>
火狐报错如下:
ReferenceError: event is not defined
if (event.fromElement.contains(event.toElement)
source = event.srcElement;//此处报错
if (event.fromElement.contains(event.toElement)//此处报错
请问怎么解决呢,这个到google浏览器、IE8等均是正常的呢,。
------解决方案--------------------
function changeto(e) {
var a=e
------解决方案--------------------
window.event
source = a.srcElement
------解决方案--------------------
a.currentTarget;
....
}
大体改成这样试试
------解决方案--------------------
FF 。event 仅在事件发生的瞬间有效。
document.body.onclick=function(e){
e=e
------解决方案--------------------
window.event;
};