我正在使用jquery.webui-popover.js
插件。我已经让它与以下代码一起工作:
$('.button').webuiPopover({
placement: 'bottom',
title: 'Link Item to Button',
content: '123',
animation: 'pop',
delay: {
show: null,
hide: 300
},
});
但是,我试图将它包装在我自己的函数中,因为我在代码中的不同时间在多个元素上调用它。我已将其更改为:
function AddCreatePopoverBinding(element) {
element.webuiPopover({
placement: 'bottom',
title: 'Link Item to Button',
content: '123',
animation: 'pop',
delay: {
show: null,
hide: 300
},
});
}
然后在我的代码中进一步调用它:
$('.button').AddCreatePopoverBinding();
但是我收到以下错误:
Uncaught TypeError: $(...).AddCreatePopoverBinding is not a function
我做错了什么?我尝试使用 .each 语句来调用该函数,但也失败了。
最佳答案
作为A. Wolff 说,你需要所谓的 jQuery Plugin为此,即将您的函数添加到 jQuery 命名空间中:
$.fn.AddCreatePopoverBinding = function() {
return this.webuiPopover({
placement: 'bottom',
title: 'Link Item to Button',
content: '123',
animation: 'pop',
delay: {
show: null,
hide: 300
},
});
}
这也应该允许 chaining :
var button = $('.button').AddCreatePopoverBinding();
console.log(button.text());
关于javascript - 将一个函数包装在另一个函数中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29746703/