问题描述
我目前正在开发一个jQuery插件,我想解雇一个元素,但是它被解雇的方式(slideUp,hide和其他jQuery方法)指定为一个选项。
I am currently developing a jQuery plugin where I would like to dismiss an element but have the way it is dismissed (slideUp, hide, and other jQuery methods) specified as an option.
例如,有人可以指定:
$('element').plugin({style: "slideUp", speed: 300});
它将在给定元素上调用slideUp。
and it will call slideUp on the given element.
这是一个简化的例子,插件会做很多事情,但我只是想知道在JavaScript / jQuery中是否有可能这样的事情。作为一个并行,我正在寻找类似于Ruby的.send()方法的东西。
This is a simplified example and the plugin will do a lot more but I was just wondering if something like this was possible in JavaScript / jQuery. As a parallel I am looking for something much like Ruby's .send() method.
谢谢!
编辑:
我找到了这个插件它做了类似于我正在寻找的东西,但我很乐意,如果有一些方法可以做到这一点,而不包括另一个插件。
I did find this plugin which does something similar to what I am looking for but I would love if there was some way to do this without including another plugin.
推荐答案
您可以使用方括号 []
访问javascript对象的任何成员(包括函数)然后使用正常括号调用它()
。这是一个非常简化的例子
You can access any member of a javascript object (including functions) using the square brackets []
And then call that, with the normal parenthesis ()
. Here's a very simplified example
var test = {
slide: function() {alert('slide');},
fade: function() {alert('fade');}
}
var style = "slide"; // or 'fade'
test[style]();
这篇关于jQuery相当于Ruby的.send()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!