javascript - 在文本框中键入时,JavaScript/jquery 运行按钮按下事件
问题描述
我正在做一个网络应用程序,但在不激活与按键相关的 JavaScript 事件的情况下,我无法在文本框中输入内容。我尽力想办法在专注于文本框的同时禁用所有这些事件,但无济于事。有没有办法“关注”文本框以阻止其他事件运行?谢谢。
解决方案
试试下面的代码。focus
删除所有事件侦听器并onblur
添加所有事件侦听器。
let input = document.getElementById('input');
//Attach all the events
input.addEventListener('focus', removeAllEventListner);
input.addEventListener('keypress', myFunction);
input.addEventListener('blur', addEvents);
//On focus remove all the events
function removeAllEventListner() {
input.removeEventListener('keypress', myFunction);
console.log('Event removed');
}
//Onblur add all event
function addEvents() {
input.addEventListener( 'onkeypress', myFunction);
console.log('Event attached');
}
function myFunction() {
console.log('Keypress event');
}
<input type="text" id="input" value="" >
希望这能解决问题。
推荐阅读
- arrays - 如何使用多处理从 python 中的数据帧创建 gzip 文件
- r - 如何按顺序逐组更新
- kotlin - 在 Kotlin 中,如何创建类名是变量的类的实例?
- python - XML 解析尊重中间的空元素 ("
") 在 Pyhton/BeautifulSoup
- sql - 仅从感兴趣的表中选择
- struct - 有没有办法让递归结构/枚举默认装箱?
- for-loop - 来自命令的批量变量存储结果
- c++ - 没有执行策略的 std::transform_reduce 是否可移植?
- python - Keras 上的自定义损失函数给出了一个奇怪的错误:__call__() 缺少 1 个必需的位置参数:'y_true'
- python - 图片更新后显示空白