【发布时间】:2017-04-23 10:34:58
【问题描述】:
我在使用 Firefox(32.0 版)运行此代码时遇到问题
<!DOCTYPE html>
<html>
<head>
<title>Test A</title>
<script>
function showCoords(evt){
alert(
"clientX value: " + evt.clientX + "\n" +
"clientY value: " + evt.clientY + "\n"
);
}
function begin(){
parag = document.getElementById("parag");
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: 事件未定义
相反,这段代码在 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) 中存在问题。 提前谢谢你。
注意:不要告诉我更新浏览器(我必须使用它)也不要使用 jquery 或类似的。
【问题讨论】:
-
试试
parag.addEventListener("click", function (event) {showCoords(event); }, false);你需要传递事件对象。 developer.mozilla.org/en-US/docs/Web/API/EventListener
标签: javascript html firefox mouseevent