我在一个开启('模糊')听众。 $(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。
答案 0 :(得分:1)
您无需传入。由于您已将其存储在闭包中的变量中,因此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
}
}
}
答案 1 :(得分:0)
发现了问题。我在myRequest.onload语句块中错过了一个分号,导致了不稳定的结果。
答案 2 :(得分:0)
如果您无法访问变量,只需增加其范围,使其可以从代码的任何部分访问:
use Icicle\Coroutine\Coroutine;
use Icicle\Dns\Executor\Executor;
use Icicle\Dns\Resolver\Resolver;
use Icicle\Loop;
use Icicle\Socket\Client\Connector;
$generator = function () {
try {
$resolver = new Resolver(new Executor('8.8.8.8'));
// Coroutine pauses until yielded promise is fulfilled or rejected.
$ips = (yield $resolver->resolve('example.com'));
$connector = new Connector();
// Coroutine pauses again until yielded promise is fulfilled or rejected.
$client = (yield $connector->connect($ips[0], 80));
echo "Asynchronously connected to example.com:80\n";
} catch (Exception $exception) {
echo "Asynchronous task failed: {$exception->getMessage()}\n";
}
};
$coroutine = new Coroutine($generator());
Loop\run();
在此var myObject = $("#gameListDiv");
myObject.on('blur','td', function(){
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
。