当 event.target 为 XMLHttpRequest 时,获取活动 DOM 元素

发布于 2024-11-25 19:17:10 字数 1616 浏览 0 评论 0原文

我需要获取单击元素的属性值,但我通过单击有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋日私语 2024-12-02 19:17:10

更改

function showDescription(XMLObj){

function showDescription(XMLObj, target){

然后更改

descParLi[i].addEventListener('click', function(event) {loadAsync('/description/', showDescription)}, false);

descParLi[i].addEventListener('click', function(ev) {loadAsync('/description/', function(request) { showDescription(request, ev.target); })}, false);

然后在 showDescription 中使用 target


此更改捕获在闭包中单击按钮时的事件,然后在回调中使用该事件。换句话说,请注意回调函数如何使用 ev 参数 - 调用回调时会保留 ev 的值,及其 target< /code> 属性(您想要的 DOM 元素)然后被传递到 showDescription 中。

Change

function showDescription(XMLObj){

to

function showDescription(XMLObj, target){

Then change

descParLi[i].addEventListener('click', function(event) {loadAsync('/description/', showDescription)}, false);

to

descParLi[i].addEventListener('click', function(ev) {loadAsync('/description/', function(request) { showDescription(request, ev.target); })}, false);

And then use target inside showDescription.


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 of ev is preserved when the callback is called, and its target property (the DOM element you want) is then passed into showDescription.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文