我有一个按钮点击生成的事件,它会引发XMLHttpRequest。但是,我发现一旦启动XMLHttpRequest,原始事件就会设置为null。为什么会这样?有没有办法保留原始事件?
document.getElementById('start').addEventListener('click', handler);
function handler(e){
var req = new XMLHttpRequest();
var method = 'GET';
var destination = 'example.com';
req.open(method, destination);
req.send();
console.log(e.currentTarget) // shows <button></button>
req.onreadystatechange = function(){
console.log(e.currentTarget) // shows null!!! Why???
}
}
&#13;
<form>
<button id='start' type='button'>Click</button>
</form>
&#13;
答案 0 :(得分:0)
试试这样:
function handler(e){
const buttonEvent = e;
var req = new XMLHttpRequest();
var method = 'GET';
var destination = 'example.com';
req.open(method, destination);
req.send();
console.log(e.currentTarget) // shows correct current target
req.onreadystatechange = function(){
console.log(buttonEvent);
console.log(buttonEvent.target)
}
}