doudouwd2017 2015-03-02 14:18
浏览 51
已采纳

Javascript事件监听器让我困惑

I'm confused. I want to construct a gallery wherein you can click on any image and you have the full-scale image superimposed on the document (kinda like with facebook galleries).

The gallery's thumbnails are displayed with some PHP. It looks like this:

$i = 0;

while($img_dir = mysqli_fetch_assoc($gallery_q))
{
    $i++;

    echo
    '
        <div class="entry"><div class="image" id="' . $img_dir['dir'] . '">
<img id="img' . $i . '" src="' . $img_dir['thumb_dir'] . '" /></div></div>
    ';

}

I then fetch the number of images with json_encode and produce a bunch of event listener with a loop, like so:

for(var i = 1; i <= nbImg; i++)
{ 
    document.getElementById("img" + i).addEventListener("click", display(i));
}

The display function being the following:

function display(i)
{
document.getElementById("body").insertAdjacentHTML("afterBegin",
'<div id="display"><div><img src="' + document.getElementById("img" + i).parentNode.getAttribute("id") + '"></div>');

style.insertAdjacentHTML("beforeend",
"#display{z-index: 20; position: fixed; width: 1000px; height: 1000px; left: 50%; top: 10%; margin-left: -600px;");

}

And well, it works.. except display() is called without any regards for whether I click on the image or not. But when I restrain this behavior to only one image (that is, the event listener isn't in a loop and no argument is passed to the function; it just displays a preset image) then all is fine. The event is triggered when I click.

It's actually not the first time I encounter this phenomenon of the listening function being triggered not matter what event I ask the event listener to be on the lookout for. It happens most often with alert and I honestly can't figure out why. Can someone help me?

  • 写回答

2条回答 默认 最新

  • duandaotuo5542 2015-03-02 14:20
    关注

    You can't assign the function like that. Adding the () immediately invokes the function (to pass the function, omit the () - but you'll also lose the ability to pass i in, event would instead be passed in since it's the first param that the function passed to addEventListener has):

    document.getElementById("img" + i).addEventListener("click", function() {
        display(i);
    });
    

    You'll probably notice that i is always the last iterated loop var, so you want to wrap this in a closure:

    for(var i = 1; i <= nbImg; i++) { 
        (function(i) {
            document.getElementById("img" + i).addEventListener("click", function() {
                display(i)
            });
        })(i);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?