質問

I'm creating a JQuery plugin which takes in parameter a String : handler.

Depending on that, my plugin will be launched. Example if : "handler = submit"

In my plugin I'll call :

$("form").submit(function() {
// Code inside
});

With handler = "focusOut"

$("form").submit(function() {
// Code inside
});

I've tried :

 $("form").this[handler](function()
 $("form").window[handler](function() 

But no results. Any idea ? Thanks for the help :)

役に立ちましたか?

解決

If it's an event

var handler = 'submit';

$("form").on(handler, function() {

});

If it's a jQuery method

var handler = 'hide';

$('#element')[handler]();

or with a callback

var handler = 'fadeIn';

$('form')[handler](function() {

});

$() returns an object, so you can use either dot or bracket notation to reference the method, which would be a property of the object etc. so just replace the dot notation with the appropriate bracket notation instead.

If it's an event, you're better of using on(), but you could also use bracket notation to reference the method directly, like above.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top