简体   繁体   English

向addEventListener注册的Chrome事件未在“点击”时触发

[英]Chrome event registered with addEventListener not firing on 'click'

I developed a small web app using js. 我使用js开发了一个小型Web应用程序。 The app is working as expected in mozilla ff browser. 该应用程序在mozilla ff浏览器中可以正常运行。 On window load triggers an ajax request, which, on success alters the dom - includes a small select element and adds options to it, each of them getting an eventListener. 在窗口加载时触发ajax请求,成功后会更改dom-包括一个小的select元素并为其添加选项,每个选项都获得一个eventListener。 Here are some relevant extracts: 以下是一些相关摘录:

var lst = null;
window.onload=init;
function init() {
$.ajax({
    data : {
        type : 'someType',
        sender : 'someSender'
    },
    beforeSend : function () {
    },
    success : function (response) {
        response = JSON.parse(response);
        if (response.links.constructor === Array) {
            dispNavList(document.getElementById("navProxy"), response.links);
            atch();
        }
        if (response.data.constructor === Array) {
            dispTable(document.getElementById("dataProxy"), response.data);
            if (response.data.length > 0) {
                setTimeout("refresh()", 1000);
            }
        }
    },
    complete : function () {
    }
});}
function dispNavList(proxy, v) {
if (lst != null) {
    dtch();
    lst = null;
}
proxy.innerHTML = null;
if (v.length > 0) {
    lst = document.createElement('select');
    var i = 0;
    while (i < v.length) {
        var opt = document.createElement('option');
        opt.setAttribute('value', v[i].ename);
        opt.appendChild(document.createTextNode(v[i].dscr));
        lst.appendChild(opt);
        i = i + 1;
    }
    proxy.appendChild(document.createElement("p").appendChild(document.createTextNode('Data fetched @:' + timenow())));
    proxy.appendChild(lst);
} else {
    proxy.appendChild(document.createElement("p").appendChild(document.createTextNode('Data fetch failed @:' + timenow())))
}}
function atch() {
if (lst != null) {
    var options = lst.querySelectorAll("option");
    if (options.length != "undefined") {
        [].forEach.call(options,
            function (e) {
                                console.log('inside');
            e.addEventListener('click', caller,false);//.bind(e), false);
        })
    }
}}
function caller(event) {
    console.log(event.target.nodeType);
    return;
/*var arr = {
    type : 'd',
    sender : this.getAttribute('value')
};
$.ajax({
    data : arr,
    beforeSend : function () {
        clearTimeout(aTout);
        aTout = null;
    },
    success : function (response) {
        clearTimeout(aTout);
        aTout = null;
        response = JSON.parse(response);
        if (response.links.constructor === Array) {
            dispNavList(document.getElementById("navProxy"), response.links);
            atch();
        }
        if (response.data.constructor === Array) {
            dispTable(document.getElementById("dataProxy"), response.data);
            //alert(response.data.length);
            if (response.data.length > 0) {
                setTimeout("refresh()", 1000);
            }
        }
    },
    complete : function () {
        }
    }
});*/}

dtch() function is the reverse of atch() removing the eventlisteners. dtch()函数与atch()删除事件侦听器相反。 There are some comments there because i tried first of all to bind this object to the option (in function atch()) and it successfully does so while caller function didn't have at first an argument (event - i've tried this following examples from online books), it just recognized this object appropriately (in ffox). 那里有一些注释,因为我首先尝试将此对象绑定到选项(在atch()函数中),并且它成功地做到了,而调用方函数起初没有参数(事件-我已经尝试过以下操作) (例如在线图书中的示例),它只是适当地识别了该对象(冒充)。 The problem is that caller() isn't invoked on click despite the fact that the eventListener is properly attached (checked it in developer tools). 问题在于,尽管eventListener已正确连接(在开发人员工具中已选中),但单击时未调用caller()。 If something's missing (code, other info etc.) please ask and i'll provide them. 如果缺少某些内容(代码,其他信息等),请询问,我将提供它们。 Also please ignore other commented code, the app is under construction and i'm trying various things. 还请忽略其他注释的代码,该应用程序正在构建中,我正在尝试各种方法。 Even if there are small syntax errors i guarantee that i have a working code for ffox. 即使有小的语法错误,我也保证我有ffox的有效代码。

Sorry for posting a duplicate question. 很抱歉张贴重复的问题。 You can get the answer @ addEventListener working in firefox but not in chrome . 您可以在firefox中获得答案@ addEventListener,但不能在chrome中使用 So alter the event type to 'change' on the select element and get option selected through 因此,将事件类型更改为select元素上的“ change”,并通过以下方式获取选项

selectElement.options[selectElement.selectedIndex].value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM