只是为了稍微更新DashK的非常好的答案:从 jQuery 1.7 开始,.delegate() 已被 .on() 取代(请参阅此处)。
$('a.foo').popover({
html: true,
title: 'Hello',
placement: 'bottom',
content: '<button id="close-me">Close Me!</button>'
}).parent().on('click', 'button#close-me', function() {
console.log('World!');
});
在此处查看 jsfiddle:http: //jsfiddle.net/smingers/nCKxs/2/
在将 .on() 方法链接到 $('a.foo'); 时,我也遇到了一些问题。如果您遇到此类问题,请尝试将其添加到文档、正文或 html 中,例如:
$('a.foo').popover({
html: true,
title: 'Hello',
placement: 'bottom',
content: '<button id="close-me">Close Me!</button>'
});
$('body').on('click', 'button#close-me', function() {
console.log('World!');
});