2011-01-04 4 views
0

내가 태그를 클릭하여 트리거되는 숨겨진 DIV 보여주기 위해 노력하고 있어요 :JQuery와 토글 HTML 텍스트

버튼 코드는 다음과 같습니다 : 여기

<a href="#" onclick="return false;" class="viewMoreProducts">View Related Products</a> 

내가를 클릭하면 내 JQuery와입니다 버튼 : 나는 또한 사업부가 표시되고있는 경우 변경하려면 링크 텍스트를 변경하려면 잘하지만 작업 숨겨진 사업부의 전환을 얻을 수 있습니다

// Show Hidden Product Boxes on Expand - More Products and More Link 
$(".viewMoreProducts").click(function() { 
    $(this).parent().parent().parent().find(".moreProductsBox:first").slideToggle(300, function(){ 
     if ($(this).parent().find(".moreProductsBox").is(":visible")){ 
      $(this).find(":input").addClass("visibleCounter"); 

     } 
     if ($(this).parent().find(".moreProductsBox").is(":hidden")){ 
      $(this).find(":input").removeClass("visibleCounter"); 
     } 
    }); 
}); 

는, 그때는 "관련 제품을 숨기기했다 보여 "및 광고 게재 무섭게하는 아이콘. 그런 다음 내가 접히면 관련 제품보기로 돌아가서 더하기 아이콘을가집니다.

이미 jquery에 추가하는 방법은 없나요?

이것은 꽤 똑바로 들리지만 내게 주위를 도망칩니다.

감사

답변

2

당신은이 같은 (다중 부모 (가정)의 더 단순화 할 수없는)을 시도해 볼 수도 있습니다 조금 구식 일 수 있습니다.

var x = this.innerHTML; 
this.innerHTML = x.replace("show","z").replace("hide","show").replace("z","hide"); 
+0

감사합니다. – estern

1
<a href="#" class="viewMoreProducts">View Related Products</a> 

jQuery를 :

$(".viewMoreProducts").click(function(){ 
    var that = this; // reference to the <a> 
    $(this).parent().parent().parent() 
     .find(".moreProductsBox:first").slideToggle(300, function(){ 
     var isvisible = $(this).parent().find(".moreProductsBox").is(":visible"); 
     $(this).find(":input").toggleClass('visibleCounter', isvisible); 
     $(that).html((isvisible ? 'Hide It' : 'Show It')); 
     }); 
}); 
+0

이게 제대로 작동하지 않는 것 같습니다. 숨겨진 div를 열면 닫히지 않고 클릭해도 텍스트가 바뀌지 않습니다. – estern

+0

@estern 아마도 if 문 방정식이 'true'로 해석되지 않고 '$ (this) .is ("visible")'를 시도하고 if 문의 첫 번째 코드 블록 (사실) 그것이 필요한 시점에 실행되고 있는지 알려주십시오. –

+0

@Marcus Whybrow : if와 else 모두에 경고를 추가 했으므로 아무 것도 나타나지 않습니다. 선언문이 옳은 것을 부르지 않는다면 소리가납니다. – estern

0

:

$(".viewMoreProducts").click(function() { 
    var $button = $(this); 
    $(this).parent().parent().parent().find(".moreProductsBox:first").slideToggle(300, function() { 
     var $input = $(this).find(":input"); 
     if ($(this).is(":visible")) { 
      $input.addClass("visibleCounter"); 
      $button.text('Hide Related Products'); 
     } else { 
      $input.removeClass("visibleCounter"); 
      $button.text('View Related Products'); 
     } 
    }); 
    return false; // allows you to remove that onclick attribute 
});