在下面的代码中,.live()
不起作用,但 .autocomplete()
起作用。为什么? (参见http://jsfiddle.net/fRpCy/1/)
var input = [];
input.push($('input'));
$(input).live('keydown', function (event) {
console.log('You have pressed a key!');
});
$(input).autocomplete({ source: ['test1', 'test2'] });
最佳答案
.live()
查询对象方法对本质上是选择器的查询进行操作。 $([$('input')])
不是选择器查询,它是对包含选择器查询的数组的查询。
来自the docs :
<小时/>... the
.live()
method should always be called directly after a selector ...
从我的评论中复制:
.autocomplete()
之所以有效,是因为它的操作更符合其他 jQuery 函数,这些函数枚举查询提供的对象并立即对它们进行操作。 .live()
不会枚举,而是检查稍后的查询 - 每当触发任何事件时。由于查询不是它所期望的形式,因此它会忽略它。
关于jquery - `.live()` 和 `.autocomplete()` 之间不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7277758/