2011-01-04 11 views
0

私は、タグのクリックによってトリガーされる隠されたdiv要素を表示しようとしています:私がクリックしたときにここでjqueryのトグルhtmlのテキスト

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

がための私のjQueryのです:

ボタンのコードは次のようになりますボタン:私は隠されたdiv要素のトグルがうまく働いて得ることができます

// 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"); 
     } 
    }); 
}); 

が、私もそれは「関連製品を非表示にしている表示するには、div要素が表示されているときに変更するには、リンク上のテキストを変更したいです"と広告を表示ifferentアイコンが崩壊する。それから私がそれを崩壊させると、それはView Related Productsを読むために戻り、プラスのアイコンを持っています。

私が既に持っているjqueryに追加する方法はありますか?

これはかなりまっすぐに聞こえますが、周りに走り回っています。

おかげ

答えて

2

あなたは(sがさらに簡素化することができません)複数の​​親を仮定して()は、このような何かを試みることができる:

$(".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

これはうまくいきました – estern

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

のjQuery:

$(".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 
}); 
+0

これは動作していないようです。それは隠しdivを開き、私はそれを閉じることはできませんし、テキストをクリックしても変更されません。 – estern

+0

@estern if文の方程式が 'true'に解決されていない可能性があります。' $(this)).is( "visible") 'を試して、if文の最初のコードブロックに警告を出しますtrue)、それが実行されるべきかどうかを私に知らせてください。 –

+0

@Marcus Whybrow:ifとelseの両方にアラートを追加し、どちらもポップアップしません。宣言が正しいことを呼んでいないのと同じように聞こえる。 – estern

0

これはちょっと古風な感じです:

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