切换添加显示无?

Bre*_*ent 3 jquery toggle

我正在尝试创建一个框但是当单击一个列表项时它会下降然后在单击时返回.我以前做了很多次,但由于某种原因,Jquery正在添加display:none;

        $('li.item-162').toggle(function(){
                           $('.login').animate({
                                top: '27px'
                                 }, 1000);

                        },function() {          
                             $('.login').animate({
                                top: '-212px'
                                 }, 1000);
                        });

        $(".li.item-162").show();
Run Code Online (Sandbox Code Playgroud)

在firebug我的代码显示 在此输入图像描述 你可以在这里看到它.. http://rdhealthandsafety.co.uk/index.php

任何想法为什么会发生这种情况,我猜它是我周二早上的蓝调:(

ins*_*ere 6

jQuery 1.8起,您打算使用该.toggle()函数的方式弃用,并已在jQuery 1.9中删除.

类别:不推荐1.8:

.toggle()将两个或多个处理程序绑定到匹配的元素,以便在备用点击上执行.

这是jQuery 1.9中的注释更改中的原因:

这是.toggle()的"单击运行指定函数的元素"签名.它不应与.toggle()的"更改元素的可见性"相混淆,而不是已弃用.前者被删除以减少混淆并提高库中模块化的可能性.jQuery Migrate插件可用于恢复功能.

尽管如此,Gaby又名G. Petrioli创建的函数与使用什么而不是toggle(…)jQuery> 1.8的答案相同:

$.fn.toggleClick = function(){
    var methods = arguments, // store the passed arguments for future reference
        count = methods.length; // cache the number of methods 

    //use return this to maintain jQuery chainability
    return this.each(function(i, item){
        // for each element you bind to
        var index = 0; // create a local counter for that element
        $(item).click(function(){ // bind a click handler to that element
            return methods[index++ % count].apply(this,arguments); // that when called will apply the 'index'th method to that element
            // the index % count means that we constrain our iterator between 0 and (count-1)
        });
    });
};
Run Code Online (Sandbox Code Playgroud)

您可以像这样简单地使用它:

$('selector').toggleClick(function1, function2, […]);
Run Code Online (Sandbox Code Playgroud)