简体   繁体   中英

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. Can i just create a cascaded div tag and turn it one into a contenteditable one or is there any other options? 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/

It should work if you simply replace onblur with onfocusout . They are similar, but only the second one bubbles.

Attach a listener to the parent div and it should work

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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