&引用;“未定义”不是一个函数;在jQuery插件中
我试图在jQuery中编写一个插件,但在这一部分中我得到了“undefined不是一个函数”:&引用;“未定义”不是一个函数;在jQuery插件中,jquery,Jquery,我试图在jQuery中编写一个插件,但在这一部分中我得到了“undefined不是一个函数”: (function($) { $.fn.build = function(options){ var settings = $.extend({ x: 0, y: 0, type: 0 }, options); $.post("/build", { x: settings.
(function($) {
$.fn.build = function(options){
var settings = $.extend({
x: 0,
y: 0,
type: 0
}, options);
$.post("/build", { x: settings.x, y: settings.y, type: settings.type}, function(data) {
// Return JSON array
if(data['doBuild'] == 'true') {
this.append("<div class='item' style='left:"+settings.x+"px; top:"+settings.y+"px;'></div>"); // ERROR ON THIS LINE
}
},'json');
return this;
};
}(jQuery));
(函数($){
$.fn.build=函数(选项){
变量设置=$.extend({
x:0,,
y:0,
类型:0
},选项);
$.post(“/build”,{x:settings.x,y:settings.y,type:settings.type},函数(数据){
//返回JSON数组
如果(数据['doBuild']='true'){
this.append(“”;//此行出错
}
}“json”);
归还这个;
};
}(jQuery));
我认为这与“this”语句有关,但我不理解其中的错误。为什么不起作用
此
引用了$.post
的回调。看起来您只需要保存对外部的引用,此:
var that = this;
$.post("/build", { x: settings.x, y: settings.y, type: settings.type}, function(data) {
// Return JSON array
if(data['doBuild'] == 'true') {
that.append("<div class='item' style='left:"+settings.x+"px; top:"+settings.y+"px;'></div>");
}
},'json');
我怀疑!我只是不知道如何引用外部的“this”。。。我应该教你如何将它保存到变量中。谢谢,我现在就试试!
$.post("/build", { x: settings.x, y: settings.y, type: settings.type}, $.proxy(function(data) {
// Return JSON array
if(data['doBuild'] == 'true') {
this.append("<div class='item' style='left:"+settings.x+"px; top:"+settings.y+"px;'></div>");
}
}, this),'json');