我对我的众多错误之一有一个根本性的误解。我使用jquery。
我有一个对象定义为:
var terms = {};
terms.clear_history = function(a, b)
{ /* DO SOMETHING */ }
我可以调用terms.clear_history(1,2)
在我的主 js 文件中运行函数,没问题。但是当我尝试通过 <a/>
的“点击”来调用它时元素:
$(document).on('click', '#clearterms', function(){
terms.clear_history(1, 2);
});
它给了我以下错误:
Uncaught TypeError: Object # has no method 'clear_history'
我知道我不明白这里的一些基本内容......
谢谢!
最佳答案
这听起来像是一个范围问题。也许全局范围内的术语与给定方法分配的clear_history相同。
另外,您不想将参数命名为 this
,这是 JS 中的保留关键字。
试试这个:
window.terms = {};
window.terms.clear_history = function(foo,bar){console.log(foo,bar);};
//then later:
$(document).on('click', '#clearterms', function(){
window.terms.clear_history(1, 2);
});
关于javascript - JS : call methods of an object inside jquery "click",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21079021/