gpt4 book ai didi

javascript - 如何在 XMLHttpRequest.onload = function 中访问 $(this)

转载 作者:行者123 更新时间:2023-12-03 09:53:38 27 4
gpt4 key购买 nike

我处于 on('blur') 监听器中。 $(this) 是我正在处理的 DOM 元素。当我的 XMLHttpRequest.onload 触发时,我需要继续处理 $(this)。

$('#gameListDiv').on('blur','td', function(){
var myObject = $(this);
var myRequest = new XMLHttprequest();
.
alert(myObject.attr("class")); // popup: "myClass"
.
myRequest.onload = function() {
if (myRequest.status === 200) {
// How do I pass myObject Here so I can continue working with it?
alert(myObject.attr("class")); // popup doesn't happen
}
}
}

如何“向下”传递 myObject。我需要根据 XMLHttprequest 的responseText 更改它的ID。

最佳答案

您不需要传入它。由于您已经将其存储在闭包内的变量中,因此 onload 可以访问它。直接访问即可

$('#gameListDiv').on('blur','td', function(){
var myObject = $(this);
var myRequest = new XMLHttprequest();
.
.
.
myRequest.onload = function() {
if (myRequest.status === 200) {
myObject//totally legal and same as above
}
}
}

关于javascript - 如何在 XMLHttpRequest.onload = function 中访问 $(this),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30808626/

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