开发者

jQuery delegate not working

My script:

(function($){
$.fn.megaswitcher = function(settings) {
    return this.each(function() {        
        var $i = $(this),
            current,
            childs = $i.find('.selection li').length; // returns desired number

        $i.find('.selection li').delegate('.active', 'dblclick', function(e){
            e.preventDefault();
            current = $i.find('.selection li').index(this);
            alert('triggered @ ' + current); // doesn't even execute
            var _delay = $(this).attr('name') > 0 ? (parseInt($(this).attr('name')) * 1000) : 5000;
            $(this).delay(_delay).show(0, function(){
                if((current + 1) < childs){ // if not last
                    $(this).removeClass('active').next().addClass('active').show(0, function(){
                        $i.find('.image img').addClass('tempp');
                        $(this).find('img').clone().hide().addClass('temp').appendTo($i.find('.image')).fadeIn(400, function(){
                            $i.find('.image img.tempp').remove();
                        });
                    }).trigger('dblclick');
                }else{
                    $(this).removeClass('active');
                    $i.find('.selection li:first').addClass('active').show(0, function(){
                        $i.find('.image img').addClass('tempp');
                        $(this).find('img').clone().hide().addClass('temp').appendTo($i.find('.image')).fadeIn(400, function(){
                            $i.find('.image img.tempp').remove();
                        });
                    }).trigger('dblclick');
                }
            });
        });

        $i.find('.selection li.active').trigger('dblclick');
    });     
};
})(jQuery);

Gotta admit, that, that's a huge mess over there, but I have no idea why that delega开发者_如何学Pythonte is not working...

Any ideas?

P.S. I have a different plugin based on this same technique with dblclick, that works flawlessly, except, it doesn't do item selection with find();, and I have a feeling that it's the thing that's causing the problem, but I don't know with that to replace it.

Thanks in advance!


You have to call delegate on the parent of the items that you want to attach the event. Instead of:

$i.find('.selection li').delegate('.active' ...

you should do

$i.find('.selection').delegate('li.active'
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜