简体   繁体   English

每次执行div时如何处理div图像单击事件

[英]How to handle div image click event for every time execution of div

function first(Objs) {
    var imgid= Objs;
    secondMethod(imgid);
    $('#imags_'+imgid.id).one('click', function(e) {
        alert(imgid.id)
    });    
}

function secondmethod(imgid) {
    var boxText = document.createElement("div");
    boxText.id="imags_"+imgid.id;
    boxText.innerHTML = '<div id="content" >' + '<img src=\"image.png\" width=\"50px\" height=\"50px\" id ="\images_id\" />' + '</div>';         
}
  • I refreshing(executing) first method every 10 seconds( Objs.id=1 ) . 我每10秒刷新一次(执行)第一个方法( Objs.id=1 )。
  • So its two methods executed every 10 secs, 因此,它的两个方法每10秒执行一次,
  • Example after 3 refreshes(ie 3 times executes), then when I click image I got 3 time imageid(ie alert output is 1). 示例在刷新3次(即执行3次)之后,然后单击图像时,我得到了3次imageid(即警报输出为1)。
  • how to get only one output when i clicked image with last executed object id. 当我单击具有最后执行的对象ID的图像时,如何仅获得一个输出。
  • please help me from this problem. 请帮助我解决这个问题。

Give the image a class: 给图像一个类:

boxText.innerHTML = '<div id="content" >'+        
    '<img src=\"image.png\" width=\"50px\" height=\"50px\" class ="\images_id\" />'+
    '</div>';

And use a delegated event: 并使用委托事件:

$(document).on('click', '.images_id', function() {
    alert(this.id)
});

Also, your example doesn't show it but I assume boxText is appended to the DOM at some point? 另外,您的示例没有显示它,但是我假设boxText在某个时候附加到了DOM?

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

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