FLX*_*FLX 4 jquery jquery-plugins jquery-selectors
我正在关注此文档并使用它来下载数据并将数据应用到DOM.但是,我似乎无法应用它,因为我得到了:
this.html不是函数:this.html(ajax_load);
码:
(function( $ ){
$.fn.tasks = function() {
// there's no need to do $(this) because
// "this" is already a jquery object
// $(this) would be the same as $($('#element'));
$.ajax({
url: "include/tasks_handler.php?action=gettasks&list=default",
beforeSend: function() {
this.html(ajax_load);
},
success: function(html){
this.html(html);
}
});
};
})( jQuery );
$("#taskList").tasks();
Run Code Online (Sandbox Code Playgroud)
我也尝试过$(this),它可以阻止它破坏,但它并没有将内容注入选择器.
想法?
this在ajax选项中,是指选项对象,而不是插件的上下文.解决方法是:
var that = this;
$.ajax({
url: "include/tasks_handler.php?action=gettasks&list=default",
beforeSend: function() {
that.html(ajax_load);
},
success: function(html){
that.html(html);
}
});
Run Code Online (Sandbox Code Playgroud)
这个快速示例演示了正在发生的事情:
var obj = {
foo: function() {
alert("bar");
},
bar: function() {
alert(this.foo);
}
};
obj.bar(); // function() { alert("bar"); }
Run Code Online (Sandbox Code Playgroud)
这个例子更好地展示了正在发生的事情:
var options = {
success: function(html) {
this.html(html);
},
html: function(html) {
alert("This is not the .html() you are looking for, move along." + html);
}
}
options.success("some html");
Run Code Online (Sandbox Code Playgroud)
小提琴:http://jsfiddle.net/GTScL/