怪异的“addEventListener不是函数”异常

时间:2016-03-05 02:23:27

标签: javascript google-chrome event-listener

以下代码无法在Google Chrome上正常运行:

var e,
    divStyle = {
        backgroundColor: "rgba(153, 153, 153, 0.29)",
        postion: "fixed",
        top: 0,
        left: 0,
        width: "100%",
        height: "100%"
    },
    imgStyle = {
        maxWidth: "700px",
        maxHeight: "700px"
    },
    imgDivs = document.getElementsByClassName("resumeAttchImg"),
    listener = function () {
        var style,
            img = this.firstElementChild;
        for (style in divStyle)
            this.style[style] = divStyle[style];
        for (style in imgStyle)
            img.style[style] = imgStyle[style];
    };
for (e in imgDivs) {
    imgDivs[e].addEventListener("click", listener);
    console.log(imgDivs[e].addEventListener);
}

Chrome引发了一个例外:

  

“Uncaught TypeError:imgDivs [e] .addEventListener不是函数”

但是,console.log(imgDivs[e].addEventListener)打印的功能定义如下:

  

function test.html:182 addEventListener(){[native code]}

如何解决这个问题?

0 个答案:

没有答案