将来加载的元素触发的事件 [英] event fired by a future loaded element

查看:104
本文介绍了将来加载的元素触发的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个加载图像的表格. 我需要执行一个由图像Onclick事件调用的函数.

I have a form thats loads an image. I need to do a function that is been called by the image Onclick Event.

问题在于选择器(我正在使用jQuery)找不到图像ID,因为在加载函数时图像还不存在.

The problem is that the selector ( I'm using jQuery) can't find the image id, because when the function is loaded the image wasn't yet there.

解决方案:

图片上传后->

$("#"+foto_nom).bind({
       click:function(){
       alert(this.id); }
 });

绑定事件是解决方案.

Binding an event is the solution.

推荐答案

您应该使用'on'方法将事件附加到页面加载期间不存在的选择器中.像这样:

You should use 'on' method to attach event to selectors that are not present during page load. Like this:

   $(document).on('click', '#id_of_your_image', function(e) {
    //your code here
    });

更新

在添加图像后将事件绑定到图像似乎不是一个好的解决方案.

Binding event to the image after it has been added does not seem to be a good solution.

这篇关于将来加载的元素触发的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆