2012-03-20 2 views
1

문제가 있습니다. 링크 p를 클릭하고 다른 링크를 클릭하면 작동하지 않습니다. 다시 클릭하면 작동합니다.JQuery 토글() 함수

이 내 jQuery 코드입니다 : 나는 그것을 할 수있는 방법의 초기 값은 0이어야한다 다른 링크를 클릭하면

var toggle = 0; 
$(document).ready(function() { 
    $(".feedback_block").each(function() { 
     $("a", this).click(function (e) { 
      if (toggle == 0) { 
       $(this).parent().children("p").stop(true, true).fadeIn(500); 
       $(this).addClass("clicked"); 
       $(this).children().addClass("clicked_span"); 
       toggle = 1; 
       console.log(toggle); 
      } else 
      if (toggle == 1) { 
       $(this).parent().children("p").stop(true, true).fadeOut(500); 
       $(this).removeClass("clicked"); 
       $(this).children().removeClass("clicked_span"); 
       toggle = 0; 
       console.log(toggle); 

      } 
      e.stopPropagation(); 
      return false; 
     }); 
     toggle = 0; 
    }); 
}); 

내가 매개 변수 토글 1 가져 오기를 클릭하고?

내 예 : http://jsfiddle.net/amkrtchyan/tjzaR/

+0

나는 이것을 편집했지만 S/O 편집자는 약 10 건의 추천 도서를 거부했다. 누구든지 내 (향후) 편집을 개선하십시오. – halfer

답변

1

당신은 단순히 (여기 바이올린 http://jsfiddle.net/tjzaR/1/) 당신이 할 수

$(document).ready(function() { 
    $(".feedback_block").each(function() { 
     $("a", this).toggle(function (e) { 

       $(this).parent().children("p").stop(true, true).fadeIn(500); 
       $(this).addClass("clicked"); 
       $(this).children().addClass("clicked_span"); 

      }, function(){ 
       $(this).parent().children("p").stop(true, true).fadeOut(500); 
       $(this).removeClass("clicked"); 
       $(this).children().removeClass("clicked_span"); 


      }); 
    }); 
}); 

또는 토글() (http://jsfiddle.net/tjzaR/2/)

을 사용할 수 있습니다
$(".feedback_block").each(function() { 
    $("a", this).click(function (e) { 
     if (!$(this).parent().children("p").is(":visible")){ 
      $(this).parent().children("p").stop(true, true).fadeIn(500); 
      $(this).addClass("clicked"); 
      $(this).children().addClass("clicked_span"); 

     } else{ 
      $(this).parent().children("p").stop(true, true).fadeOut(500); 
      $(this).removeClass("clicked"); 
      $(this).children().removeClass("clicked_span"); 


     } 
     return false; 
    }); 

}); 
+0

나는 toggle() 함수를 알고 있지만 내 코드에서이를 수행 할 수있는 방법이 있습니까? –

+0

@AramMkrtchyan은 위의 코드를 사용하여'$ ("a", this)) .click (function (e) {')을 제거하고 생성 한 토글 변수를 제거하십시오. – voigtan

+0

감사합니다 :) –

0
$("a", this).toggle(function() { 

       $(this).parent().children("p").stop(true, true).fadeIn(500); 
       $(this).addClass("clicked"); 
       $(this).children().addClass("clicked_span"); 
       return false; 

      }, function(){ 

       $(this).parent().children("p").stop(true, true).fadeOut(500); 
       $(this).removeClass("clicked"); 
       $(this).children().removeClass("clicked_span"); 
       return false; 

      }); 

우리가 직접 토글 기능을 사용할 수 없습니다. Toggle

+0

의 헤드가 될 것입니다.하지만 내 코드에서이를 수행 할 수있는 방법이 있습니까? –