jQuery:插件方法,我如何将东西传递给它们?
最近我一直在为我的网络项目编写很多小插件,但我仍然是插件创作的初学者。
我已经阅读了大量关于 jquery 插件的文章,但我就是无法理解方法
。
这是我当前的插件的主要外观:
(function($){
$.fn.extend({
myHelper: function(options) {
var defaults = {
some: 'defaults'
}
var options = $.extend(defaults, options);
return this.each(function() {
var o = options;
function next(target) {
// do something like slide to the next image in a slideshow
// based on the target and count passed in
slide('right');
}
function prev(target) {
slide('left');
}
function slide(direction) {
// animation for sliding in the next one
$('ul > li.current').animate({ … }, 800);
}
// doesn't exactly make sense but its just to illustrate what I need to do.
$('.next').click(next($('ul > #example'));
});
}
});
})(jQuery);
我现在所追求的,我认为方法是这里的方法......基本上是不拥有所有这些命名函数来实现微小的功能,而是定义诸如 之类的方法nextSlide
、prevSlide
、reset
、loop
我想你明白了…
你可能已经注意到了我将目标等内容传递到函数中(例如 next()
),然后将 next()
作为独立的事物调用。
我现在想开始这样做,只需让一个方法完成所有这些工作,并使我能够在我的 plugin.js
文件中编写类似 $(o.nextBtn 的内容).nextSlide();
例如。
但是,如何编写一个接受参数(例如本例中的 target
)的方法呢?
到目前为止,我发现的所有方法示例和教程基本上都没有接受方法函数参数的选项。这可能吗?
理想情况下,我想让这些函数也可以在我的插件中链接,但这开始让我无法理解。
如果您知道如何创建一些东西,以整齐有序的方式完成我想要完成的任务,或者有一个值得我看的好教程,请分享。
谢谢。
lately I have been writing a lot of tiny plugins for my web projects but I am still very much a beginner in plugin authoring.
I have been doing a lot of reading on jquery plugins but I simply cannot wrap my head around methods
.
Here is what my current plugins mostly look like:
(function($){
$.fn.extend({
myHelper: function(options) {
var defaults = {
some: 'defaults'
}
var options = $.extend(defaults, options);
return this.each(function() {
var o = options;
function next(target) {
// do something like slide to the next image in a slideshow
// based on the target and count passed in
slide('right');
}
function prev(target) {
slide('left');
}
function slide(direction) {
// animation for sliding in the next one
$('ul > li.current').animate({ … }, 800);
}
// doesn't exactly make sense but its just to illustrate what I need to do.
$('.next').click(next($('ul > #example'));
});
}
});
})(jQuery);
What I am after now, and I thought method was the way to go here… is to basically not have all these named functions for tiny bits of functionality and instead define methods such as nextSlide
, prevSlide
, reset
, loop
i think you get the idea…
You may have noticed that I pass things such as the target into the function (next()
for example) and then call the next()
as a standalone thing.
I now want to start doing this by simply having a method do all this and enable me to write within my plugin.js
file something like $(o.nextBtn).nextSlide();
for example.
But how do I write a method that accepts arguments such as the target
in this example?
All the examples and tutorials for methods I have found so far basically didn't have an option of accepting arguments into the method's function. Is this even possible?
Ideally I'd like to make these functions chainable within the my plugin as well but this is starting to get way over my head.
If you know how to create something that does what I want to accomplish in a neat and organized manner or have a good tutorial I should look at, please share.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
构建 jQuery 插件的推荐方法是将所有方法放入一个数组中,然后使用插件的 main 函数来访问它们。
例如,使用您的一些示例:
在这个示例中,blah 是代表您的插件的对象,然后您使用插件的方法来调用函数并传入值。
The recommended way to build a jQuery plugin is to actually place all of your methods into an array, and then use the plugin's main function to access them.
For example, using some of your example:
In this example, the blah is the object representing your plugin, and then you use the plugin's method to call functions and pass in the values.