简体   繁体   English

如何检测文本框是否在 div 标签下?

[英]How to detect if textbox is under a div tag?

Everything is working if i click inside the div tag and then clicking right away outside the div tag, the onblur event is functioning well but when i try to click first the textbox and then clicking the outside div, the onblur event does not trigger right away.如果我在 div 标签内单击然后立即在 div 标签外单击,一切正常,onblur 事件运行良好但是当我尝试先单击文本框然后单击外部 div 时,onblur 事件不会立即触发. Can i just create a cascaded div tag and turn it one into a contenteditable one or is there any other options?我可以只创建一个级联的 div 标签并将其变成一个可编辑的标签,还是有任何其他选项? Here's my code这是我的代码

<div tabindex="0" onblur="alert('This one has tabindex');">
   <input type="text"> This will receive focus
</div>

Link http://jsfiddle.net/3kw9aqsg/链接http://jsfiddle.net/3kw9aqsg/

It should work if you simply replace onblur with onfocusout .如果您只是将onblur替换为onfocusout ,它应该可以工作。 They are similar, but only the second one bubbles.它们很相似,但只有第二个起泡。

Attach a listener to the parent div and it should work将监听器附加到父 div,它应该可以工作

<div tabindex="0" id="myDiv">

document.getElementById('myDiv').addEventListener('blur', listener, true)
function listener(){ alert('from parent');}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM