开发者

Dynamically call jQuery function

开发者 https://www.devze.com 2023-03-27 17:38 出处:网络
I want to do this: var todo = \"text\"; $this.eval(todo).split(/\\b[\\s,\\.-:;]*/).length; So that this would be t开发者_JS百科he resulting function:

I want to do this:

var todo = "text";

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

So that this would be t开发者_JS百科he 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!

0

精彩评论

暂无评论...
验证码 换一张
取 消