在javascript中引用Event对象

时间:2015-09-05 17:59:27

标签: javascript mouseevent

如果我在谷歌浏览器浏览器上运行它,

下面的代码工作正常,但它没有在Firefox上

在firefox中未定义事件对象。那么在这种情况下如何获取对事件对象的引用

<html>
<body>

<p>Click the button to display the date.</p>

<button onclick="displayDate()">The time is?</button>

<script>
function displayDate(e) {
if (!e) var e = window.event;
alert(e.target.innerText);
    document.getElementById("demo").innerHTML = Date();
}
</script>

<p id="demo"></p>

</body>
</html> 

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

对于Firefox,您必须将事件传递给函数:

<button onclick="displayDate(event)">The time is?</button>

<script>
  function displayDate(e) {
    document.getElementById("demo").innerHTML = Date();
  }
</script>

http://jsfiddle.net/k6cge4a0/1/