的ReferenceError:不定义的事件 - 火狐
我有这段代码运行与Firefox的一个问题(32.0版)的ReferenceError:不定义的事件 - 火狐
<!DOCTYPE html>
<html>
<head>
<title>Test A</title>
<script>
function showCoords(evt){
alert(
"clientX value: " + evt.clientX + "\n" +
"clientY value: " + evt.clientY + "\n"
);
\t }
\t function begin(){
\t parag = document.getElementById("parag");
\t parag.addEventListener("click", function() {showCoords(event); }, false);
}
</script>
</head>
<body onload = "begin()">
<p id="parag">To display the mouse coordinates click in this paragraph.</p>
</body>
</html>
它可以在Chrome和其他浏览器,但是当我运行它与Firefox 32.0它给我这个错误:
ReferenceError: event is not defined
相反,该代码在Firefox工程没有错误:
<!DOCTYPE html>
<html>
<head>
<title>TestB</title>
<script>
function showCoords(evt){
alert(
"clientX value: " + evt.clientX + "\n" +
"clientY value: " + evt.clientY + "\n"
);
}
</script>
</head>
<body>
<p onclick="showCoords(event)">To display the mouse coordinates click in this paragraph.</p>
</body>
</html>
谁能告诉我,为什么以前的代码不能正常工作,而后者呢? 同样,这两款产品都在Chrome中运行,但第一款是Mozilla Firefox(32.0)中的bug。 预先感谢您。
注意:不要告诉我更新浏览器(我必须使用它),也不要使用jquery或类似的。
不知道它是否在Firefox中工作,因为我不想使用Firefox。
<!DOCTYPE html>
<html>
<head>
<title>Test A</title>
<script>
function showCoords(evt){
alert(
"clientX value: " + evt.clientX + "\n" +
"clientY value: " + evt.clientY// + "\n"
);
\t }
function begin(){
parag = document.getElementById("parag");
parag.addEventListener("click", function(e) {showCoords(e);}, false);
}
</script>
</head>
<body onload="begin()">
<p id="parag">To display the mouse coordinates click in this paragraph.</p>
</body>
</html>
这个技巧!非常感谢你!还感谢@ gurvinder372谁评论我的问题。 – leoll2
尝试发送事件的开始功能像这样开始(事件),然后将其发送到在JS侧
而且在JS的showCoords函数接受事件时,U可以试试这个, 事件= e || windows.event所以你可以肯定,所有的浏览器得到覆盖
在begin()中传递事件并不是我真正想要的,因为begin()应该仅用于添加事件倾听该段落。 无论如何,我仍然尝试过你的解决方案,但没有奏效。 Thx无论如何 – leoll2
Firefox不会对全球window.event
可用,即使是最新的版本(53.0),而Chrome了。
尝试'parag.addEventListener(“click”,function(event){showCoords(event);},false);'你需要传递事件对象。 https://developer.mozilla.org/en-US/docs/Web/API/EventListener – gurvinder372