如果我在 google chrome 浏览器上运行下面的代码,它可以正常工作,但在 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>
感谢您的帮助
最佳答案
对于 Firefox,您必须将事件传递给函数:
<button onclick="displayDate(event)">The time is?</button>
<script>
function displayDate(e) {
document.getElementById("demo").innerHTML = Date();
}
</script>
关于javascript - 在 JavaScript 中引用 Event 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32416118/