开发者

jquery: get parent element from ajax call

开发者 https://www.devze.com 2023-01-12 11:19 出处:网络
How to make this code work? I don\'t see how I can reach div from within $.get callback. $(\"<div/>\", {

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

精彩评论

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

关注公众号