DOM树中所有子元素的一个监听器
one listener for all child elements in the DOM tree
如何只有一个keypress
事件,以便它可以由DOM树中的任何子元素触发。
例如,我有这样的东西:
<table>
<tr>
<td><input type="text" id="col_1" value="1"/></td>
</tr>
<tr>
<td><input type="text" id="col_2" value="2"/></td>
</tr>
<tr>
<td><input type="text" id="col_3" value="3"/></td>
</tr>
</table>
例如,当用户更改id=col_3
和id=col_2
上的值时,我如何区分是哪个输入触发了此事件?我需要能够将input
、id
和它的value
保存在array
中,以便以后可以读取。
您可以尝试使用jQuery.on方法,
$("table").on("keypress", "input", function(event){
alert($(this).attr("id"));// gets the input id
alert($(this).val());// gets the input value
});
此代码将处理<table>
标记内的所有输入。
如果你不想在每次击键时都执行这个监听器,给你一段时间(3秒)的喘息时间,试试这个代码-
var timeoutReference;
$("table").on("keypress", "input", function(event){
var el = this; // copy of this object for further usage
if (timeoutReference) clearTimeout(timeoutReference);
timeoutReference = setTimeout(function() {
doneTyping.call(el);
}, 3000);
});
$("table").on("blur", "input", function(event){
doneTyping.call(this);
});
function doneTyping(){
var el = this;
// we only want to execute if a timer is pending
if (!timeoutReference){
return;
}
// reset the timeout then continue on with the code
timeoutReference = null;
//
// Code to execute here
//
alert('This was executed when the user is done typing.');
alert($(el).attr("id"));//id
alert($(el).val());//value
}
相关文章:
- 监听器必须是一个函数
- JQuery:所有按钮只使用一个监听器有什么缺点吗
- javaScript-我们如何将一个事件监听器设置为另一个事件监听器的从属
- 是否可以在单独的谷歌地图瓦片中添加一个监听器
- 如何创建一个jQuery监听器来遍历一些标记
- DOM树中所有子元素的一个监听器
- 我想为一个元素添加一个事件监听器,当相应的元素滚动到视图中时,该监听器就会被激发
- 为什么生成器的下一个函数不能是事件监听器的回调函数?
- 如何使用一个函数来设置一个监听器回调
- 如何删除Hammer.js 2.0中的事件监听器,并将识别器绑定到一个新函数
- dblclick的事件监听器导致鼠标移动的事件不起作用,并显示一个带有一条线的圆圈
- 在React组件卸载时移除一个事件监听器
- 你能重写“onscroll”吗?使用另一个监听器
- Node.js发送到一个特定的监听器
- 为每行绑定一个监听器,jquery
- Node.js EventEmitter:如何绑定一个类上下文到事件监听器,然后移除这个监听器
- 用CanJS为文档上触发的事件设置一个全局监听器/控制器
- 我如何得到一个事件监听器的工作上的元素是添加到我的html通过javascript
- 一个页面上有多个jQuery事件监听器
- 返回一个从NodeJS监听器派生的值