用jquery.on()替换jquery.live()不起作用 [英] replacing jquery.live() with jquery.on() doesn't work
问题描述
我有几个文本框,在ajax调用后会动态添加.这些框通过.live()绑定到当前正常工作的事件处理程序.我想用更新的.on()函数替换它.
I have several textboxes that are added dynamically after an ajax call. These boxes are tied to event handlers with .live() that currently work fine. I want to replace that with the newer .on() function.
这就是我所拥有的
$('.MyTextBox').live({
mouseenter: function () { .... },
mouseleave: function () { .... },
blur: function () { ... }
});
当我用.on()替换.live()时,它不起作用;添加文本框后,它们不会显示其正常行为.
When I replace .live() with .on() it doesn't work; the textboxes don't display their normal behavior after they're added.
如果我写$('#MainDiv .MyTextBox').live({...
,其中MainDiv是发生所有操作的顶级DOM元素,那么也不会发生任何事情.
If I write $('#MainDiv .MyTextBox').live({...
, where MainDiv is the top DOM element where all the action happens, nothing happens either.
我想念什么?
谢谢
推荐答案
这里有示例,但有委托:
Heres the example they have, but with the delegate:
与非授权: http://jsfiddle.net/HDtz3/1/
这就是为什么我讨厌新的语法.
This is why i hate the new on syntax.
您只需执行以下操作:
$('#MainDiv').on({
mouseenter: function () { .... },
mouseleave: function () { .... },
blur: function () { ... }
}, '.MyTextBox');
它应该可以工作
这篇关于用jquery.on()替换jquery.live()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!