【问题标题】:Binding multiple event handlers after ajax loadajax 加载后绑定多个事件处理程序
【发布时间】:2012-12-22 13:25:39
【问题描述】:

我正在使用 .load() 方法将一些内容加载到 html div 中。

$("#cards").load("/cards/?offset="+offset);

新内容包含几个我想绑定事件处理程序的<a>。首先,我使用的是.on() 方法:

$('body').on('click', '#excerpt-slide', function() { alert('Link 1 Clicked'); } 

当我尝试将第二个事件处理程序绑定到不同的<a> 时,单击链接不会触发匿名函数:

$('body').on('click', '#add-count', function() { alert('Link 2 Clicked'); }

我是否需要取消绑定单击事件处理程序才能使其正常工作?

请帮忙。谢谢...

【问题讨论】:

  • 你确定你有一个id为add-count的锚
  • 我们需要查看更多您的代码...如果元素选择器正确,它没有理由不工作

标签: ajax jquery javascript-events event-handling


【解决方案1】:

您可能缺少一个 ID 为 add-count 的锚点。为了您的信息,您不必取消绑定事件。 jQuery 可以为一个元素处理多个事件。

您的代码中缺少关闭 )

$('body').on('click', '#add-count', function() { alert('Link 1 Clicked'); } ); 

$('body').on('click', '#add-count', function() { alert('Link 2 Clicked'); } );

check the demo

我只能通过代码和您提供的详细信息来判断

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    相关资源
    最近更新 更多