jQuery: using live() with each()
I have this function here:
$("a.fancybox_vid").each(function(){
$(this).fancybox({
titleShow : false,
width: 640,
height: 395,
autoDimensions: false,
overlayOpacity: 0.6,
href: "misc/mc.php?v="+$(this).attr('id')开发者_如何学JAVA
});
});
Now a link with c lass .fancybox_vid gets appended, and then this will not work. Only if it is there from the beginning. How can I have live() in this each().
If you want "live-like" functionality for methods, you can use the livequery
plugin:
$(function() {
$('a.fancybox').livequery(function() {
$(this).fancybox({
titleShow : false,
width: 640,
height: 395,
autoDimensions: false,
overlayOpacity: 0.6,
href: "misc/mc.php?v="+$(this).attr('id')
});
});
});
...although it would be better (less overhead) to just call the fancybox
plugin on the newly created elements.
This is not possible, as live
handles events. Fancybox only creates events on the current element queue.
I think you have to apply the .fancybox()
method every time you create a new element.
What about doing a live event, that calls the fancybox:
$("a.fancybox_vid:not(.fancy)").live('click', function(){
$(this).addClass('fancy').fancybox({
titleShow : false,
width: 640,
height: 395,
autoDimensions: false,
overlayOpacity: 0.6,
href: "misc/mc.php?v="+$(this).attr('id')
});
});
精彩评论