jQuery live(),点击了 1 次鼠标,但我得到了 2 个事件?
我的 javascript 发生了一些奇怪的事情。我从服务器端获取数据,当结果返回时,我用表中的数据更新我的 div。 然后,我调用 postUpdate 并将一个函数绑定到表中的每一行。
jQuery.fn.postUpdate = function(id) {
console.log('postUpdate with id: '+id);
if (id == "myContent") {
jQuery('#DataTable tr').live(
'click',
function(event) {
jQuery.fn.showDetails(event);
});
}
};
jQuery.fn.showDetails = function(event) {
console.log(event);
};
但是,每次我单击该行时,我都会将事件打印两次?
Object { originalEvent=Event click, type="click", timeStamp=351233505, more...} base.js (line 181)
Object { originalEvent=Event click, type="click", timeStamp=351233505, more...} base.js (line 181)
这是怎么回事? jQuery 中的错误?
I'm getting some weird thing happening with my javascript. I'm fetching data from the server side, and when the result come back, I update my div with the data in my table.
I then call postUpdate and bind a function to every rows in the table.
jQuery.fn.postUpdate = function(id) {
console.log('postUpdate with id: '+id);
if (id == "myContent") {
jQuery('#DataTable tr').live(
'click',
function(event) {
jQuery.fn.showDetails(event);
});
}
};
jQuery.fn.showDetails = function(event) {
console.log(event);
};
However, everytime I click on the row, I'm getting the event printed twice?
Object { originalEvent=Event click, type="click", timeStamp=351233505, more...} base.js (line 181)
Object { originalEvent=Event click, type="click", timeStamp=351233505, more...} base.js (line 181)
What's the deal here? Bug in jQuery?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每次调用
postUpdate
时,您都会添加一个额外的实时处理程序。使用
live
后,您不应在每次添加元素时重新添加处理程序,因为处理程序将应用于所有匹配的元素。Every time you call
postUpdate
, you add an additional live handler.Once you're using
live
, you should not re-add handlers every time you add elements, since the handlers will apply to all matching elements.