2012-07-08 79 views
1

我试图使用jQuery 1.5.2未来的元素上附加一个focusin event。只是为了澄清,this has also been attempted using jQuery 1.7.2 with the same issue.的focusIn用活不工作-jQuery

事件在不触发,因为我预料到。然而,类似的click事件的作品使用.live

我在这里组建一个例子给你看看我的代码正确。希望这不会是一个简单的问题(尽管它似乎总是!)。

Link to jsfiddle

这是我focusIn事件我试图连接

$(".active").live("focusin", function() { 
    $(this).text("focusin using live"); 
}); 

我相信我已经找到了一些相关的问题,但即时通讯无法使用它们来解决我的代码。我更愿意解释一下“这里是你的代码更正的答案”。

如果你觉得我需要更多的信息添加到我的问题,请发表评论。

相关

jQuery focusin and focusout live events are not firing

Why the "focusin" event handler isn't called?

+2

你为什么使用这样一个老的jQuery版本? – ThiefMaster 2012-07-08 10:43:59

+0

是我正在努力的一个老化的网站。这也不适用于jQuery 1.7.2。看到这里(http://jsfiddle.net/5T3SG/4/) – Undefined 2012-07-08 10:46:21

回答

1

你需要,如果你希望在focusin事件来触发关注的元素。事实上,你正在使用DOM元素.active类并不意味着你正在关注它。

$('.active').live('focusin', function() { 
    $(this).text('focusin using live'); 
}); 

$('.active').focus(); 
​ 

Here's a demo

你会发现另一件事是,.live()支持focusin事件从jQuery的1.7.1开始。显然,在这个版本的jQuery,.live()已被废弃,你应该使用.on()

$(document).on('focusin', '.active', function() { 
    $(this).text('focusin using on'); 
}); 

$('.active').focus();