javascript - 在其他 jQuery 插件中调用自定义 jQuery 插件

标签 javascript jquery twitter-bootstrap

我正在使用 Twitter Bootstrap 的 .popover jQuery 插件。我正在编写自己的使用 .popover 的 jQuery 插件,但控制台显示 this“没有方法‘popover’”。这是我的代码:

$.fn.mypopover = function(msg){
    this.click(function(){
        this.popover({title:"Static title",content:msg,trigger:'manual'}).popover('show');
    });
}

最佳答案

$.fn.popover 函数中,thispopover() 被调用的 jQuery 对象。但是,在 this.click() 的回调中,this 是触发点击事件的元素。每次您在另一个 function 中时,this 都会不同(基于函数的调用方式)。

您需要在点击事件中执行$(this).popover()

$.fn.mypopover = function(msg){
    // "this" is a jQuery object
    this.click(function(){
        // "this" is a DOM element
        $(this).popover({title:"Static title",content:msg,trigger:'manual'}).popover('show');
    });
}

关于javascript - 在其他 jQuery 插件中调用自定义 jQuery 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22512643/

相关文章:

javascript - 将参数从 JQuery 发送到 PHP 文件

jquery - 为什么这个 toggleClass() 不起作用?

css - 尝试实现与 airbnb.com 相同的轮播

javascript - 使用括号和逗号进行不熟悉的变量赋值

javascript - 是否有用于淡化文本框提示的 jQuery 插件?

javascript - HTML Canvas 允许用户从文件加载图像

html - 为什么 Bootstrap 没有转换为响应我的静态导航顶部?

jquery - Bootstrap 左侧可折叠导航菜单

javascript - 有没有办法通过javascript检测关闭页面?

javascript - 树屋 jQuery 挑战