简体   繁体   中英

How can I get the focus event trigger?

Using only javascript, I'd like to know if a focus event was triggered by a click on a "select" tag, or if it was triggered by a tab. Is it possible?

you can check it like this:

 const select = document.getElementById("number"); select.addEventListener("keyup", myFunction); function myFunction(e) { if(e.key === 'Tab'){ console.log('I am a focus event was triggered by using tab') } } select.addEventListener("mousedown", myFunction2); function myFunction2(){ console.log('I am a focus event was triggered by a click') }
 <label for="cars">Choose a number:</label> <select name="cars" id="number" > <option value="volvo">1</option> <option value="saab">2</option> <option value="opel">3</option> <option value="audi">4</option> </select>

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