0

arindic使用document.ready().

然后在它们的动态生成结束后,仍然在document.ready()例程中,我将这些元素与keypress事件绑定,如下所示:

$('.arindic').bind('keypress', function(evt){
   // do lots of stuff
});

但它不起作用。我知道“做很多事情”的功能运行良好,因为在底部document.ready()我有这个:

 $('.arindic').keypress(function(evt){ // do lots of stuff // });

这适用于页面上已经静态存在的文本区域。我绑定动态元素的方式有问题吗?而不是.bind()我也尝试过.on().live()但无济于事。

4

1 回答 1

2

最好的办法是使用.on()绑定到静态元素并冒泡到动态创建的元素。

$(document).on('keypress', '.arindic', function(event)
{
    // do lots of stuff
});

使用文档绑定不是一件好事,我会将其更改.arindic为包含在其中的最高级别的静态元素,并允许此事件从那里冒泡。

于 2013-03-17T10:55:00.147 回答