开发者

Slight confusion of `this` in a JavaScript call back function

$.ajax({url: path_to_file, cache: false, success: function(html_result){
    $("#window_" + this.id + "_cont_buffer").html(html_result);})

Now then. This function call is with in开发者_如何学C a function of a class. this.id is a property of said class. will this pass the function value of this.id into the string the anonymous function, or will it try to evaluate it when the function actually gets called, thus making no sense.

If this is not going to work how I want it to, can you recommend how I achieve this.


In the particular case of $.ajax(), this can be specified using the context attribute. So, Matthew's solution gives you the this that is specified in the function that you make the $.ajax function call from.

You can see the jQuery documentation for more information on setting the this for the success callback.


By default this will be an internal jQuery object. However, you can override that by explicitly specifying context: this as part of the call. Then, this will be the object you're calling it from.

$.ajax({url: path_to_file, context: this, cache: false, success: function(html_result){
    $("#window_" + this.id + "_cont_buffer").html(html_result);})

will do what you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜