我正在使用 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
函数中,this
是 popover()
被调用的 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/