Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery plugins 创建jquery插件:this.html不是一个函数_Jquery Plugins_Jquery_Jquery Selectors - Fatal编程技术网

Jquery plugins 创建jquery插件:this.html不是一个函数

Jquery plugins 创建jquery插件:this.html不是一个函数,jquery-plugins,jquery,jquery-selectors,Jquery Plugins,Jquery,Jquery Selectors,我正在跟踪并使用它下载数据并将其应用于DOM。然而,我似乎无法应用它,因为我得到: this.html不是一个函数:this.html(ajax\u load) 代码: 我还尝试了$(this),它阻止了它的崩溃,但它没有将内容注入选择器 想法?ajax选项中的这个指的是选项对象,而不是插件的上下文。解决办法是: var that = this; $.ajax({ url: "include/tasks_handler.php?action=gettasks&list=defau

我正在跟踪并使用它下载数据并将其应用于DOM。然而,我似乎无法应用它,因为我得到:

this.html不是一个函数:this.html(ajax\u load)

代码:

我还尝试了$(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);
    }
});
此快速示例演示了正在发生的情况:

var obj = {
    foo: function() {
        alert("bar");
    },
    bar: function() {
        alert(this.foo);
    }
};

obj.bar(); // function() { alert("bar"); }
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");
此示例更好地演示了正在发生的情况:

var obj = {
    foo: function() {
        alert("bar");
    },
    bar: function() {
        alert(this.foo);
    }
};

obj.bar(); // function() { alert("bar"); }
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");

Fiddle:

您可能想先修复您的自动执行函数 然后为html创建一个变量,然后再尝试将其传递到另一个函数中

(function ($) {

      $.fn.tasks = function () {

            var ele = $(this);

            $.ajax({
                    url: "include/tasks_handler.php?action=gettasks&list=default",
                    beforeSend: function() {
                           ele.html(ajax_load);
                    },
                    success: function(html){
                           ele.html(html);
                    }
            });
      };

}( jQuery )); // Bracket goes after

使用
$(this)
使其成为具有
.html
方法的jQuery对象。将随后发生的事情作为问题发布(因为这是实际问题:)@FLX:this您试图指的是什么?此外,
this
在发送前的回调范围内不可用。。您必须执行
var somevar=this
使其在回调中可见——有趣的
这是范围规则。