gpt4 book ai didi

javascript - 在 JavaScript 中引用 Event 对象

转载 作者:行者123 更新时间:2023-11-27 23:54:05 25 4
gpt4 key购买 nike

如果我在 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>

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

关于javascript - 在 JavaScript 中引用 Event 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32416118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com