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");
精彩评论