我正在编写一个简单模板的插件,虽然我还没有开始......
我一直收到以下错误:
Uncaught TypeError: Object function (e,n){return new x.fn.init(e,n,t)} has no method 'tmpl'
目前的插件代码是:
$.fn.tmpl = function(template, options)
{
var defaults = {
file: '',
callback: function(){}
}
var options = $.extend(defaults, options);
function parse()
{
}
};
这就是我调用函数的方式:
var template = $.tmpl('user-profile-feed-item');
我将jQuery 2.0.1包含在此文件的引用之上,并且正在加载此文件,但它只是不想工作,而且我没有想法。
提前致谢。
答案 0 :(得分:2)
你想创建jQuery函数而不是插件。它可能是这样的
$.tmpl = function(template, options)
{
var defaults = {
file: '',
callback: function(){}
}
var options = $.extend(defaults, options);
function parse()
{
}
};
答案 1 :(得分:1)
的 jsFiddle Demo without errors
强> 的
您将tmpl
附加到jQuery原型。如果实例化jQuery对象,则只能访问原型。换句话说,你需要调用jQuery
var template = $().tmpl('user-profile-feed-item');
修改
的 jsFiddle Demo
强> 的
以下是您使用的代码示例,可能有助于指出扩展jQuery原型的一些用法。
$.fn.tmpl = function(template, options)
{
var me = this;//this will refer to the current jQuery object, console.log(this) to see more
var defaults = {
file: '',
callback: function(){}
};
var options = $.extend(defaults, options);
defaults.parse = function()
{
alert(me.html());
};
return defaults;
};
var template = $("#d").tmpl('user-profile-feed-item');
template.parse();