gpt4 book ai didi

javascript - jquery:从ajax调用获取父元素

转载 作者:行者123 更新时间:2023-11-30 09:07:47 25 4
gpt4 key购买 nike

如何使这段代码起作用?我看不出如何从 $.get 回调中到达 div

$("<div/>", {
text: "some text",
click: function (e) {
$.get("bar.php", function(data) {
$(this).text(data); // doesn't work
});
}
}).appendTo("body");

最佳答案

在保存对 DIV 的引用的点击处理程序中创建一个变量,并在 $.get 回调中使用该变量。

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

关于javascript - jquery:从ajax调用获取父元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3521151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com