当 event.target 为 XMLHttpRequest 时,获取活动 DOM 元素
我需要获取单击元素的属性值,但我通过单击有一个ajax请求,在本例中,event.target是XMLHttpRequest而不是单击的DOM元素。在这种情况下如何获取点击的 DOM 元素?
// Parsing and makeup.
function showDescription(XMLObj){
var parser = new DOMParser(),
XMLDoc = XMLObj.responseText,
parsedXMLDoc = parser.parseFromString(XMLDoc, 'text/xml'),
eventTarget = event.target,
descContent = document.getElementById('description') ;
// Conditional actions.
};
};
// Create request.
function loadAsync(url, callback) {
var request;
if(window.XMLHttpRequest) {
request = new XMLHttpRequest();
request.open("GET", url, true);
request.send(null);
request.onreadystatechange = requestProcess;
} else if(window.ActiveXObject){
request = new ActiveXObject('Microsoft.XMLHTTP');
if (request) {
request.open("GET", url, true);
request.send(null);
request.onreadystatechange = requestProcess;
}
}
function requestProcess() {
if (request.readyState == 4) {
if (request.status == 200) {
if (typeof callback == 'function') {
callback(request)
}
}
}
};
};
// Define event listener for each menu element.
function showDescListener(){
var descPars = document.getElementById('description-paragraphs'),
descParLi = descPars.getElementsByTagName('li');
for (var i = 0; i < descParLi.length; i++){
descParLi[i].addEventListener('click', function(event) {loadAsync('/description/', showDescription)}, false);
}
};
I need to get attribute value of clicked element, but I have an ajax request by click and in this case event.target is XMLHttpRequest instead of clicked DOM element. How can I get the clicked DOM element in this situation?
// Parsing and makeup.
function showDescription(XMLObj){
var parser = new DOMParser(),
XMLDoc = XMLObj.responseText,
parsedXMLDoc = parser.parseFromString(XMLDoc, 'text/xml'),
eventTarget = event.target,
descContent = document.getElementById('description') ;
// Conditional actions.
};
};
// Create request.
function loadAsync(url, callback) {
var request;
if(window.XMLHttpRequest) {
request = new XMLHttpRequest();
request.open("GET", url, true);
request.send(null);
request.onreadystatechange = requestProcess;
} else if(window.ActiveXObject){
request = new ActiveXObject('Microsoft.XMLHTTP');
if (request) {
request.open("GET", url, true);
request.send(null);
request.onreadystatechange = requestProcess;
}
}
function requestProcess() {
if (request.readyState == 4) {
if (request.status == 200) {
if (typeof callback == 'function') {
callback(request)
}
}
}
};
};
// Define event listener for each menu element.
function showDescListener(){
var descPars = document.getElementById('description-paragraphs'),
descParLi = descPars.getElementsByTagName('li');
for (var i = 0; i < descParLi.length; i++){
descParLi[i].addEventListener('click', function(event) {loadAsync('/description/', showDescription)}, false);
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改
为
然后更改
为
然后在
showDescription
中使用target
。此更改捕获在闭包中单击按钮时的事件,然后在回调中使用该事件。换句话说,请注意回调函数如何使用
ev
参数 - 调用回调时会保留ev
的值,及其target< /code> 属性(您想要的 DOM 元素)然后被传递到
showDescription
中。Change
to
Then change
to
And then use
target
insideshowDescription
.This change captures the event at the time the button was clicked in a closure, which is then used in the callback. In other words, notice how the
ev
parameter is used by the callback function -- the value ofev
is preserved when the callback is called, and itstarget
property (the DOM element you want) is then passed intoshowDescription
.