2011-09-01 45 views
0

我有一些品牌,我显示,但我想只显示前5,并已看到更多的品牌文字。我已将所有设置正确并且功能正常,但点击后我想从更多品牌更改文字以查看更少的品牌。jQuery改变文字onclick问题

<li style="font-size:12px; margin-left:2px;" class="showmorebrands"> 
<a onclick="$('#morebrands').toggle('fast', function() { });">Show More Brands</a></li> 

我试过内部文本,但更改文本和HTML,所以我的按钮不工作了。

我感谢所有帮助。

谢谢。

编辑:

morebrands有内联样式显示:无;

+0

如果是ID为'元素morebrands'? – Shef

+0

这些是隐藏的品牌。我应该将其更改为$(“.moremorebrands”)吗? –

回答

4

试试这个

$(function(){ 
    $("li.showmorebrands a").click(function(){ 
     var $this = $(this); 
     $('#morebrands').toggle('fast', function() { 
     if($('#morebrands').is(":visible")){ 
      $this.html("Show More Brands"); 
     } 
     else{ 
      $this.html("Show Less Brands"); 
     } 
     }); 
    }); 
}); 
+0

错字'visisble'。 – Shef

+0

谢谢@shef欣赏! – ShankarSangoli

0

尝试:

$('#morebrands').toggle('fast', function() { $(this).text('Show less brands') }); 
+0

这是错误的。在切换回调方法中,'this'将指向'#morebrands'而不是锚点。 – ShankarSangoli

+0

,将每一个隐藏的品牌改变为“显示较少的品牌” –

+0

如果您修改答案,我可以删除投票。 – ShankarSangoli

1

我会做这样的事情:

<li style="font-size:12px; margin-left:2px;" class="showmorebrands"> 
    <a>Show More Brands</a> 
</li> 
<script type="text/javascript"> 
    $(function() { 
    var $moreBrands = $('#moreBrands'), 
     $showHideBrands = $('.showmorebrands a'); 

    $showHideBrands.click(function(event) { 
     $moreBrands.toggle(
     'fast', 
     function() { 
      $showHideBrands.text(
      $moreBrands.is(':visible') ? 'Show Less Brands' : 'Show More Brands'); 
     } 
    ); 
     event.preventDefault(); 
    }); 
    }); 
</script> 
+0

你快,我在写它。这是做到这一点的正确方法。功能分开介绍。 – Shef

+0

嗯,它可以很好地工作,但是当它变成“显示较少品牌”后点击它时,它不会变回以显示更多品牌 –

+0

@FishBasketGordo:尝试添加一个'preventDefault()'。 – Shef