jQuery live(),点击了 1 次鼠标,但我得到了 2 个事件?

发布于 2024-12-13 17:22:23 字数 789 浏览 4 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷你仙 2024-12-20 17:22:23

每次调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文