简体   繁体   English

我如何让jquery识别使用.on在fly元素上创建的?

[英]How do I get jquery to recognize created on the fly element with .on?

I have the following code I am using to begin an animation process: 我使用以下代码来开始动画处理:

$('body').on('click','#contain span', function(){
            var $target = $(this).parent().children('#contain_inner');
            flyout.fadeOldBox($target);
            flyout.createBox();
})

flyout.createbox() then creates a new instance of #contain_inner but that new instance is not available when my click function above tries to target it again. 然后, flyout.createbox()创建一个#contain_inner的新实例,但是当我上面的click函数尝试再次定位该新实例时,该新实例不可用。 I know that if I were to use .live things would work but that is deprecated now and I don't really understand how I would use .on to accomplish what I need here. 我知道,如果我使用.live ,那么一切都会正常进行,但是现在已经过时了,我真的不明白如何使用.on来完成我在这里需要的工作。

I know I am using .on above already for the click, but I still don't understand how to bind it to the new #contain_inner div that will be create in flyout.createBox() . 我知道我已经在上面使用.on进行点击了,但是我仍然不知道如何将其绑定到将在flyout.createBox()创建的新#contain_inner div上。

EDIT: showing flyout.createBox() code...all is working now! 编辑:显示flyout.createBox()代码...现在都在工作!

createBox: function(){
            $box = $('<div id="feed_contain_inner"></div>'); //was accidentally creating that as div class= instead of the id
            $box.load('example')
            $box.appendTo('#contain');
            flyout.positionBox($box);

    }

这与现场直播相同:

$(document).on('click', '.yourslector', callback);

Are you setting your .on() inside $(document).ready() ? 您是否在$(document).ready()设置.on() $(document).ready() It will only trigger on new elements if it's set outside $(document).ready() . 如果将新元素设置 $(document).ready() 之外,则它将触发。

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

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