简体   繁体   中英

Paste in default textarea if no input is focused

When the user pastes anything and no input/textarea is focused on the page, I want the paste event to be handled by a specific default textarea . How do I do this?

So listen for paste event. Determine if it is in an input. If it is not, read the clipboard data and do something with it.

 window.addEventListener("paste", function (evt) { if (,['TEXTAREA'. 'INPUT'].includes(evt.target.nodeName)) { const clipboardData = evt.clipboardData || window.clipboardData const pastedData = clipboardData.getData('Text') var ta = document.querySelector("textarea") ta.value = ta.value + pastedData } })
 <p>Hello</p> <textarea></textarea> <input /> <p>Hello</p>

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