且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将事件处理程序附加到 twitter 引导弹出窗口中的按钮

更新时间:2023-11-22 23:51:10

我遇到了同样的问题,就我而言,$(body).on('click') 解决方法获胜不行,因为应用程序有很多点击按钮.

I had the same problem, and, in my case, the $(body).on('click') workaround won't work, since the application has lotta click buttons.

我改为执行以下操作.这样,我们可以将委托事件的范围限制为仅弹出框元素的父元素.

I did the following instead. This way, we can limit the scope of the delegate event to just the parent of the popover element.

$('a.foo').popover({
    html: true,
    title: 'Hello',
    placement: 'bottom',
    content: '<button id="close-me">Close Me!</button>'
}).parent().delegate('button#close-me', 'click', function() {
    console.log('World!');
});

JS 小提琴:http://jsfiddle.net/dashk/5Yz7z/

附:我在我的应用程序的 Backbone.View 中使用了这种技术.这是 Fiddle 中的代码片段,以防您也在 Backbone.js 中使用它...:http://jsfiddle.net/dashk/PA6wY/

P.S. I used this technique in within a Backbone.View in my application. Here's the snippet of the code in Fiddle, in case you're using this in Backbone.js as well...: http://jsfiddle.net/dashk/PA6wY/

已编辑在 Bootstrap 2.3 中,您可以指定将添加弹出框的目标容器.现在,除了使用 .parent() 定位器,您还可以专门监听容器的事件...这可以使监听器更加具体(想象创建一个仅存在以包含弹出框的 DIV.)

EDITED In Bootstrap 2.3, you can specify a target container which popover will be added to. Now, instead of doing the .parent() locator, you can also listen to events specifically to the container... This can make the listener even more specific (Imagine creating a DIV that only exists to contain the popover.)