gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:45:50 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com