2013-02-11 36 views
2

这是一个简化版本。jquery对象执行对象的指定功能

如果我做这样的事情:

var object = [{name:"bob", type:"silly", hair:"brown", func:someFunction}, 
       {name:"greg", type:"serious", hair:"blonde", func: differentFunction}]; 

$('#thing').bind("click", object[0], handleTranslator); 

function handleTranslator(e){ 
    $([e.data.func]); 
} 

function someFunction(){ 
    console.log("clicking bob should trigger this function"); 
} 

function differentFunction(){ 
    console.log("clicking greg should trigger this function"); 
} 

当我点击#thing,它应该引发someFunction但事实并非如此。不知道为什么。

理想情况下,我希望能够从someFunction中看到(console.log)我的对象[0]。

感谢您的帮助。

+0

什么是您的HTML是什么样子? – 2013-02-11 23:27:46

+0

@AlienWebguy这是一个简单的例子。 – 2013-02-11 23:28:43

+0

@ExplosionPills#东西已经在dom – 2013-02-11 23:29:42

回答

1

你不调用你的函数:

function handleTranslator(e){ 
    e.data.func(); 
} 

演示:http://jsfiddle.net/CEdvt/

+0

未捕获的TypeError:属性'func'的对象#不是函数 – 2013-02-11 23:31:28

+0

你试过小提琴吗?有用。 – AlienWebguy 2013-02-11 23:32:07

+0

这工作...我最初尝试过,但我想我有我的功能在报价如:“differentFunction”TY – 2013-02-11 23:34:01