2014-03-19 193 views
1

我正在使用Twitter Bootstrap的.popover jQuery插件。我写我自己的jQuery插件,利用.popover,但控制台说this“没有方法'弹出'。”这里是我的代码:在其他jQuery插件中调用自定义jQuery插件

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

回答

3

里面的$.fn.popover功能,this是jQuery的对象popover()被称为上。但是,在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'); 
    }); 
} 
+0

为我工作。我在匿名函数的第一行设置了'ths = $(this);'。谢谢! – GiantDuck

+0

很高兴能帮到你:-) –