如果我点击一个按钮,activeElement
就是按钮。
如果我留下一个输入框,activeElement
它就是窗口。
如果我通过单击按钮离开输入框,则activeElement
是...两者?
为什么onfocusout
事件注册与activeElement
按钮不同?无论如何我可以从 inputbox-leave-event 的函数调用中访问 click-on-button 事件吗?
即我可以问,“你离开我是为了那个糟糕的按钮吗?”
<button type="button" onclick = "myFunction()"> button </button><br>
<input type="text" onfocusout="myFunction()"> </input>
<script>
function myFunction() {
console.log(document.activeElement);
}
</script>
该relatedTarget
事件将告诉你在哪里它留给
当然,你必须使用addEventListener
访问event
摆在首位
但我认为这展示了你想看到的
它肯定会告诉你
“你离开我是为了那个糟糕的按钮吗?”
const button = document.querySelector('button')
const input = document.querySelector('input')
function buttonHandler(e) {
console.log('button clicked');
}
function inputHandler(e) {
console.log('leaving input for', e.relatedTarget?.tagName || 'window');
}
input.addEventListener('focusout', inputHandler);
button.addEventListener('click', buttonHandler);
<button type="button"> button </button><br>
<input type="text"> </input>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句