开发者

jquery: get parent element from ajax call

How to make this code work? I don't see how I can reach div from within $.get callback.

$("<div/>", {
    text: "some text",
    c开发者_StackOverflow中文版lick: function (e) {
        $.get("bar.php", function(data) {
             $(this).text(data); // doesn't work
    });
    }
}).appendTo("body");


Create a variable inside the click handler that holds the reference to the DIV and use that variable inside the $.get callback.

$("<div/>", {
    text: "some text",
    click: function (e) {
        var $div = $(this);
        $.get("bar.php", function(data) {
             $div.text(data); // should work
    });
    }
}).appendTo("body");
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜