我对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);我有谷歌和谷歌几个小时了。有什么帮助吗。
发布于 2014-09-30 17:50:06
如果您想要实现的是在插件中添加一个静态方法,那么就像将它添加到一个普通对象中一样。
(function ($) {
// Define our base to add to
$.myNameSpace = {};
//static
$.myNameSpace.hello = function () {
console.log("HELLO");
};
})(jQuery);然后调用它,就像调用任何其他jQuery静态方法一样:
$.myNameSpace.hello();我建议你看看这个食谱
希望能帮上忙。
编辑
我想出了这个小提琴。它可能会给你一些想法。
祝好运
发布于 2014-09-30 19:10:14
如果我正确地回答了您的问题,如果您想从插件中返回一些东西,您需要从构造函数函数中返回--(来自这个问题,我无法理解您想从插件返回什么)
$.myfunc = function(options, e) {
// Default options
var settings = $.extend({}, $.myfunc.options, {
anotherfunc: function(a) {
//
}
}, options);
return /*something*/; // here
};在ajax内部发生的是,为了理解而简化了它,但是在实际实现中需要更多的技术知识。
$.ajax = function (options) {
var def = $.Deferred(); // something like this
var settings = $.extend({}, $.ajax.options, options); // possibly not exactly
// some more technical stuffs go here
return def.promise(); // jQuery XHR object which implements the promise interface
};发布于 2014-10-14 20:08:11
好的,我用简单的函数得到了一个答案,尽管它不是我曾经想要的。
test = function() {
var param = {
param1: 'hello world', // list of phone numbers to send SMS to
param2: "Hello, I have fix it and is fine", // Message to be sent
};
myfunc.send(param,function(data) {
//get the result here
javascript: console.log(data);
});
};
$(document).ready(test);这是函数
myfunc= {
send: function(param, callback) {
$.ajax({
cache: true,
url: "http://example.com/test
success: function(data) {
callback.call(this, data);
}
});
}
}它只是一个简单的函数,工作正常。
https://stackoverflow.com/questions/26125883
复制相似问题