Jquery点击第一次工作,但不是第二次

时间:2013-07-19 08:37:02

标签: jquery

我的问题就像标题说当我点击“hello number 2”div.header时我的第一次点击。第一个消失并从DOM中删除。然后在最后一个新的上升。我不明白为什么当我点击“hello number 3”标题时它没有重复相同的程序。

提前致谢。 这是一个美联储。

http://jsfiddle.net/h8S9w/2/

    var x = 3;
    var newDiv;
    var firstHeader;
    var firstInfo;
    var lastHeader;
    var lastInfo;
    $(document).ready(function(){
        $('div.header').click(pseudoakkordeon);
    });
    function pseudoakkordeon(){
            x += 1;
            active = $(this);
            firstHeader= $('div.page div.header:first-child');
            firstInfo = $('div.page div.info:first-child');
            lastHeader = $('div.page div.header:last-child');
            lastInfo = $('div.page div.info:last-child');
            $('div.info').stop(true, true).slideUp(1000);
            active.next().stop(true, true).slideDown(1000, function(){
                firstHeader.slideUp(300, function(){
                    firstHeader.remove();
                    firstInfo.remove();
                    newDivx = $('div.page').append('<div class="header"> <p>Hallihalo Number ' + x + '</p></div><div class="info"><p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hen</p></div>');

                    $(newDivx).live('click', pseudoakkordeon);
                });
            });
    };

2 个答案:

答案 0 :(得分:2)

您必须使用on方法替换click方法。所以代码现在是:

$(document).ready(function(){
    $(document).on('click', 'div.header', pseudoakkordeon);
});

请仔细阅读您的新代码:http://jsfiddle.net/h8S9w/5/

修改:我将:first-child替换为.first method。看看这个新的小提琴:http://jsfiddle.net/h8S9w/7/

答案 1 :(得分:0)

你可以改变

firstHeader.slideUp(300, function() 

$(document).slideUp(300, function()