2013-03-12 90 views
2

我想制作一个盒子,但是当点击一个列表项时,它会下降,然后单击时返回。我之前做了很多次,但由于某种原因,jquery添加了display:none;切换添加显示无?

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

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

     $(".li.item-162").show(); 

而且在萤火我的代码显示 enter image description here 你可以看到它住在这里.. http://rdhealthandsafety.co.uk/index.php

任何想法,为什么发生这种情况,我想它我周二上午蓝调:(

+2

其实这就是['.toggle ()'](http://api.jquery.com/toggle/)会:“*显示或隐藏匹配的元素。*”。 – insertusernamehere 2013-03-12 11:44:48

+0

你究竟想要什么? – supersaiyan 2013-03-12 11:46:29

+1

我认为切换是用来作为关闭? – Brent 2013-03-12 12:15:36

回答

6

您打算使用.toggle()函数的方式是已弃用jQuery 1.8已被删除jQuery 1.9

Category: Deprecated 1.8

.toggle()绑定两个或更多个处理程序来匹配的元素,以在交替的点击来执行。

这理由在Changes of Note in jQuery 1.9

这是.toggle的 “点击元素运行指定的功能” 签名()。不应该将它与“.toggle()”的“更改元素的可见性”相混淆,这不会被废弃。前者正在被删除,以减少混淆,并提高图书馆模块化的潜力。 jQuery Migrate插件可用于恢复功能。

然而Gaby aka G. Petrioli创建,做同样的回答的功能What to use instead of toggle(…) in 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) 
     }); 
    }); 
}; 

您可以简单地使用这样的:

$('selector').toggleClick(function1, function2, […]);