0

所以我的javascript有点生疏了..我正在尝试这样做:

        var images = document.getElementsByTagName("img");
        for (var i = images.length - 1; i >= 0; i--) {
            var image = images[i];
            if (image.className == "photo latest_img") {
                image.onclick = function() {
                    // here i will perform a different action depending on what image was clicked           
                    alert(image.src);
                }
            }
        };

我只是想分配一个函数处理程序,该函数应该知道单击了哪个图像。

如果我没记错的话,这是分配图像处理程序并传递该图像的引用的两步过程。

什么是最安全的跨浏览器方式来做到这一点?

4

1 回答 1

2

在函数内部使用this

image.onclick = function() {
    // here i will perform a different action depending on what image was clicked           
    alert(this.src);
}
于 2012-06-03T22:36:59.720 回答