开发者

problem with jquery's "live" function

开发者 https://www.devze.com 2023-03-28 05:57 出处:网络
the problem this works fine: $(\'#edit_curriculum .generated\').children().blur(function(){ console.log(this);

the problem

this works fine:

$('#edit_curriculum .generated').children().blur(function(){
    console.log(this);
});

but this doesn't:

$('#edit_curriculum .generated').children().live('blur', function(){
    console.log(this);
});

obs: the functions are wrapped in a $(document).ready event.


the outputs

working:

<input type=​"text" name=​"phones[]​" class=​"medium phone valid">

not working:

Uncaught Syntax error, unrecognized expression: )
k.errorjquery.js:17
k.filterjquery.js:17
kjquery.js开发者_运维知识库:17
c.querySelectorAll.kjquery.js:17
k.matchesSelectorjquery.js:17
f.extend.filterjquery.js:17
f.fn.extend.isjquery.js:17
f.fn.extend.closestjquery.js:17
Njquery.js:16
f.event.handlejquery.js:17
f.event.add.k.i.handle.kjquery.js:16
f.event.triggerjquery.js:17
ejquery.js:17


From jQuery live() documentation:

Attach a handler to the event for all elements which match the current selector, now and in the future.

This function is meant to work on a selector, not a collection of elements. I would use the following syntax:

 $('#edit_curriculum .generated > *').live('blur', function(){
   console.log(this);
 });

This selector will get any immediate children (hence what you had before) but with selection and not traversal. This should allow you to use live() as you would expect. Hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消