gpt4 book ai didi

javascript - 如何在Javascript中打印点击事件

转载 作者:行者123 更新时间:2023-11-28 05:53:38 25 4
gpt4 key购买 nike

 <body>

<button id ="b1">BUTTON</button>
</br>
<p id="text"> </p>

<script type="text/javascript">

document.getElementById("b1").onmouseout = reset_button;
document.getElementById("b1").onmousedown = mousedown_button;
document.getElementById("b1").onmouseover = mouse_over;
document.getElementById("b1").onmousemove = mouse_move;
document.getElementById("b1").onclick = click_button;


function reset_button() {
document.getElementById("text").innerHTML =
"mouseout";
}
function mousedown_button() {
document.getElementById("text").innerHTML =
"mousedown";
}
function click_button() {
document.getElementById("text").innerHTML =
"click";
}
function mouse_over() {
document.getElementById("text").innerHTML =
"mouseover";
}
function mouse_move() {
document.getElementById("text").innerHTML =
"mousemove";
}

</script>
</body>

我想在按下按钮时打印 mouseout、mousedown、mouseover、mousemove 和 onlick。然而,我编写的这段代码只打印 mouseover、mousedown 和 mouseout。有人可以解释哪一部分是错误的以及如何修复它吗?

最佳答案

您的文本被覆盖,请在此处查看控制台输出:

更新:简化的代码

(向小外星人致敬)

['mouseout', 'mousedown', 'mouseover', 'mousemove', 'click'].forEach(
ev_name =>b1.addEventListener(ev_name, e => console.log(ev_name)))
<button id="b1">BUTTON</button>

关于javascript - 如何在Javascript中打印点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37884116/

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