如何将jquery函数绑定到动态DOM [英] How to bind jquery function to a dynamic DOM
问题描述
我正在页面上使用jquery文件上传功能,如下所示:
I am using jquery file upload function on my page like following:
$('.new-variant-image')
.fileupload({
dataType: 'script',
add: function(e, data){
}
});
,但是DOM类"new-variant-image"是在页面加载后动态创建的,因此无法正常工作.我搜索了使用"on"和"live"绑定动态事件的方法,但在这种情况下无法正常工作.
but the DOM class "new-variant-image" is dynamically created after the page is loaded, so it wouldn't work. I searched ways to bind dynamic event using "on" and "live" but wouldn't get it to work in this case.
非常感谢任何帮助.
修改
随后使用AJAX创建DOM元素"new-variant-image",如果我在创建该元素后放置代码,则该元素可以工作.但是我想避免这种情况,因为当我多次使用ajax调用该函数时,它会导致浏览器挂起2至3秒钟.
The DOM element "new-variant-image" is created afterwards using AJAX, it works if I put the code after the element is created. BUT I want to avoid that, because when I call the function using ajax multiple times it cause the browser to hang about 2 to 3 seconds.
推荐答案
使用函数的回调
$('.new-variant-image').fileupload({
dataType: 'script',
add: function(e, data),
done: $('.new-variant-image').click(function(){
})
{
这篇关于如何将jquery函数绑定到动态DOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!