In the following code, .live()
don't work, but .autocomplete()
does. Why? (See 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']开发者_JAVA百科 });
The .live()
query-object method operates on queries that are fundamentally selectors. $([$('input')])
is not a selector query, it's a query over an array containing a selector query.
From the docs:
... the
.live()
method should always be called directly after a selector ...
Copied from my comment below:
.autocomplete()
works because it operates more in line with other jQuery functions, which enumerate the objects provided by the query and act on them immediately. .live()
does not enumerate, but rather inspects the query at a later time -- whenever any event is fired. Since the query is not in the form it is expecting, it simply ignores it.
.live()
works by waiting for an event to bubble up to the document
. Then it compares event.target
to the selector to determine whether or not it should run the callback function. Since you are providing an object and not a selector, it has nothing to compare so it won't run the callback. live()
does not look at the object itself except to get its selector.
.autocomplete()
operates directly on the provided jQuery object and does refer to the selector.
精彩评论