拖放插件的开发
我开始成为一名插件创建者,但还没有完全掌握它的窍门。一个问题是我想在有两个数字的情况下进行默认设置。我需要能够将它们应用到代码中。
我的意思是这样的:
var defaults = {
numbers: [1, 2]
}
我喜欢 jquery 团队如何使用draggable:
$('#drag').draggable({
grid: [1, 100]
});
这是我想做的事情。有什么想法吗?
I'm starting to become a plugin creator and don't fully have the hang of it. One problem is I would like to make an default where you have two numbers. And I need to be able to apply them to the code.
What I mean is like this:
var defaults = {
numbers: [1, 2]
}
I like how the jquery team did it with draggable:
$('#drag').draggable({
grid: [1, 100]
});
Which is something I would like to do. Any Idea how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种很好的方法是使用
$.extend
,它将两个或多个对象的内容合并到第一个对象中:现在
settings
是用户指定的选项和“默认”选项的组合。有关详细信息,请参阅插件创作教程的本节。示例: http://jsfiddle.net/2Djya/
PS: 了解插件如何工作的一个很好的资源是 jQueryUI 源 本身。
One nice way to do this is to use
$.extend
, which merges the contents of two or more objects together into the first object:Now
settings
is a combination of the user-specified options and the "default" options. See this section of the plugin authoring tutorial for more information.Example: http://jsfiddle.net/2Djya/
P.S: A good resource for figuring out how plugins work is the jQueryUI source itself.
简化的插件代码:
$.extend
将从conf
复制任何参数,并将其传递到您创建的设置默认值的对象上。simplified plugin code:
the
$.extend
will copy any params fromconf
which is passed in onto the object you create that sets the defaults.