jquery - 如何在小部件工厂插件内调用带有参数的函数?
我想在我的jquery插件(基于widget-factory)中调用一个函数。
看起来像这样:
(function( $, window) {
$.widget("mobile.multiview",$.mobile.widget, {
stackUp: function (source, event, data) {
...
}
}
}) (jQuery,this);
我想从插件外部调用 stackUp。问题:我不知道如何正确传递参数。这不起作用:
$('#trigger').multiview("stackUp('pagination', fakeEvent, fakeData)");
有人可以指出我在插件内调用公共函数并传递参数的正确语法吗?
更新:
我可以这样调用该函数:
$('#trigger').multiview('stackUp');
但是如何传递参数?
I want to call a function inside my jquery-plugin (widget-factory based).
Looks like this:
(function( $, window) {
$.widget("mobile.multiview",$.mobile.widget, {
stackUp: function (source, event, data) {
...
}
}
}) (jQuery,this);
I want to call stackUp from outside of the plugin. The problem: I don't know how to pass the parameters correctly. This does not work:
$('#trigger').multiview("stackUp('pagination', fakeEvent, fakeData)");
Can somebody point me to the correcty syntax of calling a public function inside a plugin and passing along parameters?
UPDATE:
I can call the function like this:
$('#trigger').multiview('stackUp');
but how can I pass the parameters?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,经过无休无止的干预,参数或实参是这样传递的:
你只需在函数后面添加参数,用逗号分隔即可。希望这对其他人也有帮助。
Ok, after endless meddling, the parameters or arguments are passed like this:
You just have to add parameters after the function separated by comma. Hope this helps someone else, too.
我真的不知道该插件如何扩展和公开其功能,但我猜您可以尝试以下方法之一:
但是,它们完全有可能不公开该方法。你在某处有代码吗?
更新
在真正找到这个插件是什么之后,我阅读了文档,您应该能够做到:
在此处查看源代码:http://jqueryui.com/demos/widget/default.html - 并查看定义的
random
函数。或者,您可以在更全局的范围内定义该函数,然后将其用作配置对象的一部分,并在您想要将其应用于特定元素时使用。
I dont really know how that plugin extends and exposes its functions but I would guess you could try one of the following:
However its entirely possible that they don't expose that method. Do you have the code somewhere?
UPDATE
After actually finding out what this plugin is I read up on the docs and you should be able todo:
View source here: http://jqueryui.com/demos/widget/default.html - and look at the
random
function that is defined.Alternatively you could just define the function in a more global scope and then use it as part of the config object and when you want to apply it to specific elements.
我对 jquery 不太熟悉,但试试这个:
这是一个匿名函数,将根据需要使用静态和动态参数执行。
I'm not very familiar with
jquery
but try this:This is an anonymous function which will be executed on need with your static and dynamic parameters.