简体   繁体   English

突破键盘功能

[英]Break out of a function from keyboard

The annoying thing is, I had this working. 令人讨厌的是,我已经进行了这项工作。 I cut out the code to tidy up ... but forgot to save the pasted section in text editor !! 我剪下代码整理一下...但是忘了在文本编辑器中保存粘贴的部分!

The premise is this: People type a message into a textarea. 前提是:人们在文本区域中键入一条消息。 They then click a button, which calls a function, and opens a div that displays that modified message. 然后,他们单击一个按钮,该按钮将调用一个函数,并打开一个div,其中显示了修改后的消息。 But ... I now want them to click the keyboard for the to get hidden again, (ie the keyboard triggers another function) 但是...现在,我希望他们单击​​键盘以再次隐藏它们(即,键盘触发另一个功能)

I can't use a listener / addEvent in the body tag since they are also entering detail for textarea. 我不能在body标签中使用listener / addEvent,因为它们也在输入textarea的详细信息。 Don't think it would be any good in the function as that returns as soon as it's modified the text. 不要认为它对函数有什么好处,因为一旦修改了文本,它就会立即返回。 I =THINK= I need a loop at the latter end of function so that it keeps repolling the addEvent or similar. 我= THINK =我需要在函数的最后一个循环,以便它可以继续对addEvent或类似事件进行轮询。

Or other suggestions? 还是其他建议?

I think this is what you are asking for. 我想这就是您要的。 If the user starts typing agin it will hide the preview div. 如果用户开始键入agin,它将隐藏预览div。 However, it might just be better to have an 'active' preview that evolves as they type. 但是,最好是具有一个“主动”预览,该预览会随着它们的键入而变化。

 const previewDiv = () => document.querySelector('#preview'); const textArea = () => document.querySelector('input[type="textarea"]'); const preview = () => document.querySelector('button'); document.addEventListener('DOMContentLoaded', () => { textArea().addEventListener('keydown', () => previewDiv().setAttribute('class', 'hide')); preview().addEventListener('click', () => { console.log('test'); previewDiv().setAttribute('class', 'show'); console.log(previewDiv()); previewDiv().innerHTML = textArea().value; }); }); 
 #preview { border: 1px solid black; padding: 10px; } .show { display: block; } .hide { display: none; } 
 <input type="textarea" /> <button>Preview</button> <div id="preview" class="hide"></div> 

This is a live type version of above, as the user types, we can preview output. 这是上面的实时版本,随着用户的输入,我们可以预览输出。

 const previewDiv = () => document.querySelector('#preview'); const textArea = () => document.querySelector('input[type="textarea"]'); document.addEventListener('DOMContentLoaded', () => { textArea().addEventListener('keyup', () => { previewDiv().innerHTML = textArea().value; }); }); 
 #preview { border: 1px solid black; padding: 10px; } 
 <input type="textarea" /> <div id="preview"></div> 

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

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