简体   繁体   中英

Dynamically call jQuery function

I want to do this:

var todo = "text";

$this.eval(todo).split(/\b[\s,\.-:;]*/).length;

So that this would be the resulting function:

$this.text().split(/\b[\s,\.-:;]*/).length;

I can't figure it out...How do I do this?

var todo = 'text';
$this[todo]().split(/\b[\s,\.-:;]*/).length;

If you must have it fully dynamic, then you can simply put it all in a string and just eval() it. Something like this:

var obj_name = "text";
var eval_code = "$this."+ obj_name + "(todo).split(/\\b[\\s,\\.-:;]*/).length;";
var result = eval(eval_code);

Also, don't name your variable eval to avoid conflicts with the eval function.

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM