javascript - 将一个函数包装在另一个函数中

标签 javascript jquery

我正在使用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/

相关文章:

javascript - Microsoft.Owin.Security.Facebook 与 Facebook SDK for Javascript

javascript - 在 flask 后端保存音频

javascript - 使用类型或值标签自动单击

javascript - 选择 span 标签内的文本

javascript - jquery动态创建数组

javascript - 如何使用 jquery 简化我的代码?

php - 如何使用 JavaScript 打开弹出窗口并发送 POST 变量?

javascript - AJAX - 如何保护我的 ajax/php 函数免遭垃圾邮件?

javascript - 包含冒号的 ID 的 jQuery 选择器

javascript - 为什么我可以比按数据属性更快地按类选择元素?