I'm using jQuery templates to build a list of objects. In that list items have IDs like so: stuff
For some reason when I do: $('#myid-58').fadeOut()开发者_如何学Python;
Nothing happens, it doesn't work. but if I run that same command in the browser console it works just fine. Any ideas y?
You need to make sure the elements being created by jQuery templates are already on the DOM before trying to query them with jQuery. This is why it will work in the console, as it operates on the DOM after the template has rendered.
For example, I'd imagine you don't want to fadeOut() until some event is fired, so you would need to bind the event to a parent DOM element of the template being injected that exists on the DOM prior to rendering the template.
You should use .delegate() or .live() to bind the events (delegate() is better performance-wise as the event needs to traverse less DOM elements).
精彩评论