开发者

unbind/bind not working

$(document).ready(function(){
$("li::nth-child(1)").click(function () {
      $("li").unbind('click');
       $(".content").fadeIn('slow');
       if ($("#what_image").is(':visible')) {
        $("#what_image").fadeOut('slow', function() {
        $("#what_text").fadeIn('slow'), functi开发者_开发知识库on() {
          $("li").bind('click');
          ...

The unbind works...All "li" menu elements are disabled for clicking. When the animation stops i like to bind back all the "li" elements. To no avail...Something must be wrong cos i cannot rebind the elements...


$("li").bind('click');

That line isn't telling it what to do on click. You need to bind it to the function that you want it to perform.

Assuming you want to bind it back to what it was doing before you would want this:

$(document).ready(function(){
   function FadeLi() {
      $("li").unbind('click');
      $(".content").fadeIn('slow');
      if ($("#what_image").is(':visible')) {
          $("#what_image").fadeOut('slow', function() {
              $("#what_text").fadeIn('slow'), function() {
                 $("li").bind('click', FadeLi); // Binding to the same function
              }
          }
      }
   }

   $("li::nth-child(1)").click(FadeLi);       
}


Something like this should work:

$(document).ready(function(){
  function clickHandler() {
    $("li").unbind('click');
    $(".content").fadeIn('slow');
    if ($("#what_image").is(':visible')) {
      $("#what_image").fadeOut('slow', function() {
      $("#what_text").fadeIn('slow'), function() {
        $("li").bind('click', clickHandler);
        ...
  }
  $("li::nth-child(1)").click(clickHandler);

But I have a feeling that there's a better way to accomplish what you want to than binding and unbinding the events.


Okay...I found it thanks to your help!!!

$(document).ready(function(){

 function FadeLi() {                                                                
   $("li").unbind('click');                                                         
        var index = $("li").index(this);                                            

         switch(index)                                                              
             {  case 0:                                                             
                    $("#what_image").fadeOut('slow', function() {                   
                    $("#what_text").fadeIn('slow', function() {                     
                    $("li").bind('click', FadeLi); // Binding to the same function  
                       });                                                          
                    });                                                             
                break                                                               
                case 1:                                                             
                     $("#when_image").fadeOut('slow', function() {                  
                     $("#when_text").fadeIn('slow', function() {                    
                     $("li").bind('click', FadeLi); // Binding to the same function 
                        });                                                         
                     });                                                            

                break      
                .
                .
                .                                                         
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜