Jquery 是否可以将.live()函数添加到getJson调用中?

Jquery 是否可以将.live()函数添加到getJson调用中?,jquery,live,getjson,Jquery,Live,Getjson,我有以下代码: 问题是,我无法将“active”类添加到mac div中getJson的映像中。在这里集成一个live函数会有帮助吗 $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=59597329@N08&lang=en-us&format=json&jsoncallback=?", function(data){ $.each(data.items, function(i

我有以下代码:

问题是,我无法将“active”类添加到mac div中getJson的映像中。在这里集成一个live函数会有帮助吗

$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=59597329@N08&lang=en-us&format=json&jsoncallback=?", function(data){
    $.each(data.items, function(i,item){
        $("<img src='" + item.media.m + "'></img>").appendTo("#mac");
    });
});

$("#mac img:first").addClass('active');

function slideSwitch() {
var $active = $('#mac img.active');

if ( $active.length == 0 ) $active = $('#mac img:last');

var $next =  $active.next().length ? $active.next()
    : $('#mac img.active');

$active.addClass('last-active');

$next.css({opacity: 0.0})
    .addClass('active')
    .animate({opacity: 1.0}, 1000, function() {
        $active.removeClass('active last-active');
    });
}

$(function() {
    setInterval( slideSwitch, 5000 );
});
$.getJSON(“http://api.flickr.com/services/feeds/photos_public.gne?id=59597329@N08&lang=en us&format=json&jsoncallback=?”,函数(数据){
$.each(data.items,function(i,item){
$(“”)。附件(“#mac”);
});
});
$(“#mac img:first”).addClass('active');
函数滑动开关(){
var$active=$('#mac img.active');
如果($active.length==0)$active=$(“#mac img:last”);
var$next=$active.next().length?$active.next()
:$('macimg.active');
$active.addClass('last-active');
$next.css({opacity:0.0})
.addClass(“活动”)
.animate({opacity:1.0},1000,function()){
$active.removeClass('active last active');
});
}
$(函数(){
设置间隔(滑动开关,5000);
});

你没有说你真正想做什么。一种解决方案是将必须在DOM上工作的所有代码放在
ready
处理程序中,并在加载数据后触发间隔:

function slideSwitch() {
    var $active = $('#mac img.active');
    if ( $active.length == 0 ) $active = $('#mac img:last');
    var $next =  $active.next().length ? $active.next() : $('#mac img.active');
    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(function() {
    $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=59597329@N08&lang=en-us&format=json&jsoncallback=?", function(data){
        $.each(data.items, function(i,item){
            $("<img src='" + item.media.m + "'></img>").appendTo("#mac");
        });
        $("#mac img:first").addClass('active');
        setInterval( slideSwitch, 5000 );
    }); 
});
函数滑动开关(){
var$active=$('#mac img.active');
如果($active.length==0)$active=$(“#mac img:last”);
var$next=$active.next().length?$active.next():$(“#mac img.active”);
$active.addClass('last-active');
$next.css({opacity:0.0})
.addClass(“活动”)
.animate({opacity:1.0},1000,function()){
$active.removeClass('active last active');
});
}
$(函数(){
$.getJSON(“http://api.flickr.com/services/feeds/photos_public.gne?id=59597329@N08&lang=en us&format=json&jsoncallback=?”,函数(数据){
$.each(data.items,function(i,item){
$(“”)。附件(“#mac”);
});
$(“#mac img:first”).addClass('active');
设置间隔(滑动开关,5000);
}); 
});

我不确定你想要什么。为什么不能在回调中添加类<代码>实时仅用于事件处理。此外,您的设置似乎容易出错。例如,调用回调时,您无法知道DOM已准备就绪<代码>$(“#mac img:first”).addClass('active')也不起作用。这都在一个文档中。ready函数那么您说上面发布的所有代码都在
文档中。ready
?那你为什么还有另一个(
$(function(){setInterval(slideSwitch,5000);});
)呢?@Felix,再次感谢你,你知道为什么slideSwitch不能在IE中工作吗?在Firefox中效果很好…图像加载很好,但slideSwitch从不初始化,因此不会循环浏览图像。@benhowdle89:我没有,我没有IE来测试代码。由于您实际上只使用jQuery,所以应该没有问题。嗯,我并不真的介意IE用户错过,只是好奇而已