gpt4 book ai didi

jquery函数插件,返回值类似于ajax中的$.get

转载 作者:行者123 更新时间:2023-12-01 05:46:45 24 4
gpt4 key购买 nike

我对 jquery 插件很陌生。我想创建一个函数插件,它有一个参数函数,可以像 $.get 一样向用户返回一些内容。这些是我尝试过的代码

(function($) {

$.fn.myfunc = function(options) {
if (typeof options === 'undefined') {
options = {};
}
return this;
};

$.myfunc = function(options, e) {
// Default options

var settings = $.extend({}, $.myfunc.options, {
anotherfunc: function(a) {
//
}
}, options);
};

/**
* Globally definable rules
*/
$.myfunc.options = {
param1: "",
param2: '',
}
})(jQuery);

我想在调用我的插件后实现这一目标

test = function() {
$.myfunc({
param1: "my first param",
param2: 'my second one',
anotherfunc: function(data) {
alert(data); //return something from my plugin
}
});
};
$(document).ready(test);

我已经用谷歌和重新谷歌几个小时了。请提供任何帮助。

最佳答案

如果您想要实现的目标是向插件添加静态方法,那么只需将其添加到普通对象即可。

(function ($) {

// Define our base to add to
$.myNameSpace = {};

//static
$.myNameSpace.hello = function () {
console.log("HELLO");
};
})(jQuery);

然后您可以像调用任何其他 jQuery 静态方法一样调用它:

$.myNameSpace.hello();

我建议你看看这个recipe

希望有帮助。

编辑

我想出了这个fiddle 。它可能会给你一些想法。

祝你好运

关于jquery函数插件,返回值类似于ajax中的$.get,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26125883/

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